For loop not looping?

(I'm learning to use "break" and thought to try it out with this)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string response;
    cout << "You reach a crossroads. \nEnter 1 to turn right. \nEnter 2 to turn left. \nEnter 3 to sit down." << endl;
    getline( cin, response, '\n' );
    if ( response == "1" )
    {
        cout << "You turn right..." << endl;
    }
    else if ( response == "2" )
    {
        cout << "You turn left..." << endl;
    }
    else
    {
        for ( int gametime = 0; gametime != 10; gametime++ )
        {
            cout << "You sit down and watch the breeze. \nSome time has passed." << endl;
            cout << "You reach a crossroads. \nEnter 1 to turn right. \nEnter 2 to turn left. \nEnter 3 to sit down." << endl;
            getline( cin, response, '\n' );

            if ( response == "1" || "2" || "sleep" )
            {
                break;
            }
        }
        if ( response == "1" )
        {
            cout << "You turn right..." << endl;
        }
        else if (response == "2" )
        {
            cout << "You turn left..." << endl;
        }
        else
        {
            cout << "Stop messing around hero! You've wasted a day already!" << endl;
        }
    }
}

If I enter "3" or anything that's not 1 or 2, it jumps right to "Stop messing around hero! You've wasted a day already!" instead looping.




It does work when I instead have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    else
    {
        for ( int gametime = 0; gametime < 10; gametime++ )
        {
            cout << "You sit down and watch the breeze. \nSome time has passed." << endl;
            cout << "You reach a crossroads. \nEnter 1 to turn right. \nEnter 2 to turn left. \nEnter 3 to sit down." << endl;
            getline( cin, response, '\n' );

            if ( response == "1")
            {
                cout << "You turn right..." << endl;
                break;
            }
            else if ( response == "2" )
            {
                cout << "You turn left..." << endl;
                break;
            }
            else if ( response == "sleep" )
            {
                cout << "Stop messing around hero! You've wasted a whole day by now!" << endl;
                break;
            }
            else if ( gametime == 9 )
            {
                cout << "Stop messing around hero! You've wasted a whole day by now!" << endl;
            }
        }


But I'm still curious about what I'm doing wrong in the first situation. :/
Last edited on
>Line 26 is incorrect, should be if ( response == "1" || response == "2" || response == "sleep" )

Read this for more information about control flow: http://www.cplusplus.com/doc/tutorial/control/
Thank-you very much! I hadn't known that was incorrect until now!
Topic archived. No new replies allowed.