How Do I fix the following error?

I am trying to set up a loop for a question for my c++ class. Basically, we have to set up a loop on the word Python and break the loop when it gets to h. I am getting this error at the == (error: operand types are incompatible) I got this so far,

string word = "Python";
for(int i = 0; i < word.length(); i++) {


if (word.at(i) == "h") {
break;
}
}

cout << word.at(i) <<endl;
return 0;
}

Can someone take a look at this and let me know what i'm doing wrong?
closed account (zb0S216C)
Replace "h" with 'h'.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main( )
{
    string word = "Python";
    for(int i = 0; i < word.length(); i++) 
    {
        if( word.at(i) == 'h' ) 
        {
             cout << "\'H\' reached" << endl;
             break;
        }
        cout << word.at(i) <<endl;
    }

return 0;
}
Last edited on
Sweet, that fixed that error. Now at cout << word.at(i) <<endl;

I'm getting an error at the i saying it's undefined???
'h' denotes the character h. "h" denotes a string whose only character is h. Also notice that i has no meaning outside of its scope (the for loop).
1
2
3
1>\loop.cpp(11): warning C4018: '<' : signed/unsigned mismatch
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>\Loop.exe : fatal error LNK1120: 1 unresolved externals



I got that when I used the code you posted.
closed account (zb0S216C)
What compiler are you using?
This is for a console application not a Windows application, therefore, this code will not work.
Last edited on
visual studio 2010
You have indicated to your linker that you are building a windows GUI type programme, which expects to start with a WinMain.

You need to change your settings to indicate that you're building a console executable.
Oops, my mistake.. i wasn't thinking when I created a new project. Your code worked. Thanks for the help!
Just choose "create empty project" or something to that effect when you create the project. And when you add a .cpp file to it, choose "empty file", if applicable.
Topic archived. No new replies allowed.