How to get a character from a string and compare to another string.

Hello. I need to get a character from a string and see if it is same as any other letters. HEre is what I have. I m actually Java programmer))) Began to learn C++ 2 days ago.
int main() {
string str;
cout << "Please enter a string." <<endl;
getline(cin, str);

for (int i=0;i< str.length();i++){
int count;
int x;
if (str.at(x)="e"){ //problem is here
count++;
}
cout <<"There are " <<count << " letters e " <<endl;
}
return 0;
}

Thank you.
A single character is enclosed in single quotes, i.e. 'e'. This is no different in Java.
You declared x and count in the wrong place too and you didn't initialize them.
Also = is assignment, == is comparison. You use the wrong operator in your if statement.
1
2
3
4
5
int count = 0;
for(int i =0; i<str.length();i++){
	if(str[i] == 'e')
		count++;
}
Last edited on
Topic archived. No new replies allowed.