comparing string letter by letter

I want to get a string from user and stop when they enter "*". I think it can be done with do-while (right?) the problem is then I want to check the string character by character and do some stuff with it. my problem is that I don't know how to get the compiler to do something with the string letter by letter. I tried searching for it but didn't find anything. if you can help me with find a link to an example or giving me a simple example of how to do thing with string character by character I'll be really grateful.(the thing I wanna do is checking two simple if which there is no problem with) P.S I first wanted to use something like x[n] but since there is no way to know the n I don't think it's possible if there was a way to use that I had no problem with writing the code.
You can fetch one letter at a time.

1
2
3
4
5
char inputLetter;
while ( (cin >> inputLetter) && (inputLetter!='*'))
{
  // do things
}


Or you can get the whole string and remove everything after the first '*'

std::string newString= oldString.substr(0, string1.find("*", 0));

I first wanted to use something like x[n] but since there is no way to know the n

Please explain. There are many ways to know the n.

I want to get a string from user and stop when they enter "*".

http://www.cplusplus.com/reference/string/string/getline/
1
2
std::string input;
std::getline( std::cin, input, '*' );


There are more than one way to iterate a string. Most appropriate solution depends on the "some stuff".
Last edited on
Thanks repeater but can I also do something so that it can get each letter in a new line?
New line where? In stored string or in output?

1
2
3
4
5
6
std::string input;
std::getline( std::cin, input, '*' );
// print each character with a newline
for ( auto c : input ) {
  std::cout << c << '\n';
}
Topic archived. No new replies allowed.