0
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    map<string, string> opposites;
    opposites["A"] = "T";
    opposites["T"] = "A";
    opposites["C"] = "G";
    opposites["G"] = "C";
    string line = "AGATTATATAATGATAGGATTTAGATTGACCCGTCATGCAAGTCCATGCATGACAGC";
    int promoter_index,i;
    for (i = 0; i < line.length();i++)
    {
        if (line[i] == "T" && line[i+1] == "A" && line[i+2] == "T" && line[i+3] == "A" && line[i+4] == "A" && line[i+5] == "T")
            promoter_index = i;
    }
    int start_of_sequence = promoter_index + 10;
    cout << promoter_index;

}

I am trting to find TATAAT in "line", but when i try to compare it says "ISO C++ forbids comparision between a pointer and integer". Why?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
fifiman
  • 264
  • 1
  • 3
  • 12

4 Answers4

4

std::string::operator[] returns a character, not a c-string.

if (line[i] == "T" && line[i+1] == "A"...

should be

if (line[i] == 'T' && line[i+1] == 'A'...

The single-quote denotes a character, whereas the double-quote denotes a c-string.

c-strings are of type const char[], which is implicitly converted to a const char*. chars are integral types in c++, so what the compiler is telling you is that you can't compare a pointer (the c-string "T") to an integral type (the char returned by the operator[]). The reason that the you're being told that you can't do that comparison in ISO C++ is that in the dark, brutal days of pre-standardization C++, you could do things like compare integral types and pointers for equality.

4

This is because std::string::operator[] returns a reference to a char, whereas a string literal such as "T" is a const char[N], which decays to const char* in the comparison. If you want to compare an element of line to a single character, use single quotes:

if (line[i] == 'T' && line[i+1] == 'A' ....
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

You are erroneously attempting to compare individual characters to entire strings.

Using the subscript ([]) operator on a string gives you a single character, but "T" is a string literal. (Yes, even though it only has one character.)

line[i] == "T"
/*****/   /***/
//  ^       ^
//  |       |
// char     |
//     char const[2]

Character literals are delimited by single-quotes, so you meant to write:

line[i] == 'T'

So, in context:

if (line[i]   == 'T'
 && line[i+1] == 'A'
 && line[i+2] == 'T'
 && line[i+3] == 'A'
 && line[i+4] == 'A'
 && line[i+5] == 'T')

Actually, it also has a second character which you can't see — the null terminator.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

To get your program to compile just change the the literals to ''. For example line[i] == 'T'

Alternatively you can use strcmp(&line[i], "T") != 1, to compare if the values of two characters are equal.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137