String check...!!

Hi. I am trying to check that somebody has to write a specific string at the beginning and at the end. I know my problem is in the while loop expression.
Below code only will run if string is stop not Kobe stop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
  string name;
  cout <<"Enter your name followed by stop :" ;
  getline(cin , name);
  
  while(name != "stop")
    {
      cout << "Invalid input. Type stop at the end." << endl;
      cout<<"Enter your name followed by stop: ";
      getline(cin , name);

    }

  return 0;
 
}


What if I wanted stop at the beginning or both? Thanks for the help..!!
You need to check if the user's entered string contains "stop" or not.

Rather than equating the string to "stop" i.e.
 
name != "stop"


you should use find function available in C++
http://www.cplusplus.com/reference/string/string/find/

Look at the example given in the above URL.
I don't understand the example in the link. How did he check if it was in the first or last position?
Last edited on
Topic archived. No new replies allowed.