If condition woes

I have a problem that I've tried to solve several ways, I have a feeling I am missing something small so I am going to try to see if anyone here can point me in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while ( keep_going ){
		
		cout << "Enter a word: ";
		cin.getline( input, 80, '\n' ); // Accept up to 80 characters until users press' enter and load it into input as an array.

		// I want to check if value of input is "END" if it is I want to set keep_going to false and exit the loop
		if( input[0] == 'E' && input[1] == 'N'  && input[2] == 'D' && input[3] == '/0' ){ // This does not exit at all
		if( input == "END" ){ // This does not exit at all
		if( input[0] == 'E' && input[1] == 'N'  && input[2] == 'D' ){ // This one will exit when I type in END but will also exit if I enter ENDER or anything else that starts with end.
			cout << "END command received, exiting now..." << endl;
			keep_going = false;
			break;
			}

		}


The three different if loops are not being entered at the same time this is just set up this way to demonstrate the different methods that I have tried. Is there some sort of test to try in the loop that will allow me to better test if the input was set to "END".
You wrote '/0', but you mean '\0'.
This is how you do it right:

1
2
3
4
5
6
7
8
9
10
11
for (;;)
{
    cout << "Enter a word: ";
    string input;
    getline(cin,input);
    if (input=="END")
    {
        cout << "END command received, exiting now..." << endl;
        break;
    }
}
'/0' should be '\0'.

To compare C strings you can use std::strcmp.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
I knew I had something small!

Part of the challenge is not to use any STR methods or I would use that. I actually wrote it with STR methods and am now working on removing them.

Thanks very much!
The even easier solution is to use strings.

1
2
3
4
5
6
7
string input;  // instead of a char array
getline( cin, input );  // instead of cin.getline

if(input == "END")  // nice 'n' easy
{
  //...
}


strings are your friend. char arrays are awkward.
I agree but part of this challenge is to not use any strings or* STR methods.
Last edited on
Topic archived. No new replies allowed.