Cant figure out why this happens...

Ok so Im pretty new to programming and I've started to write a simple game in a command line tool. I have one issue that seems to be popping up:
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
{  
    
    { int next;
        cout << "The Jungle Game" << endl;  
        cout << "Type 'next' to continue!" << endl;
        cin >> next;
}
         {        int next;
             cout << "Choose your path wisely as your try and escape the depths of the jungle!" << endl;
        cout << "Just be sure to remeber, 0 for left and 1 for right. Be careful for the dangers that lurk!" << endl;
        cout << "Type 'next' to continue!" << endl;
        cin >> next;
        
 }

    {int i;
        
        cout << "Left or Right?" << endl;
        cin >> i;
        if (i=0,1)
            cout << "Safe! Move to next round!" << endl;
        else 
            cout << "Safe! Move to next round!" << endl;
        
    }
    
    return 0;
}


When I run this, the first chunk of code ( lines 3-7? still new at this, i think that is the correct span.)
1
2
3
4
5
{ int next;
        cout << "The Jungle Game" << endl;  
        cout << "Type 'next' to continue!" << endl;
        cin >> next;
}

it makes all of the rest show up rather than moving to the next chunk of code;
1
2
3
4
5
6
7
 {        int next;
            {  cout << "Choose your path wisely as your try and escape the depths of the jungle!" << endl;
        cout << "Just be sure to remeber, 0 for left and 1 for right. Be careful for the dangers that lurk!" << endl;
        cout << "Type 'next' to continue!" << endl;
        cin >> next;
        
        }

Any help would be greatly appreciated. I can't figure out what I'm doing wrong as it doesn't throw any errors..
It does what?
1
2
3
4
5
{ int next;
        cout << "The Jungle Game" << endl;  
        cout << "Type 'next' to continue!" << endl;
        cin >> next;
}


Look at the type of next. Now, tell me how you're going to store the text "next" in it.
As soon as i type 'next' to satisfy bars 3-7 when I run it, it automatically runs lines 8-25 rather than stopping at line 14 for me to type 'next' again to have it continue on.

I believe it has something to do with int next; and int i;. but I don't know what exactly is causing it.
I believe it has something to do with int next; and int i;. but I don't know what exactly is causing it


Yes. int next;.

We see the type, right? int.

What kind of data does an int hold? A.. number. Is a word like "next" a number? No, it's not.

So, what happens when you try to stuff a string into a number? Your input stream goes into an error state, and doesn't give you a chance to input anything else until you clear the error state.
ok that makes sense, so what i should have done was make a string for "next" rather than an int type?

so to be sure I understand I should write that as:
1
2
3
4
String mystring;
mystring= "next"
cout << "Type 'next' to continue." << endl;
cin >> mystring;


would that be the correct coding for this? Or am I still way off?
Thanks for the help.
I don't think you need to set a value for a string like you should for int but while I don't see anything wrong with <except the missing ;>
mystring= "next"

if I was going to use it, I would use
mystring= "exit";

The user changes it right afterwards so either one should be ok.

As for
1
2
3
4
5
        int i;
        cout << "Left or Right?" << endl;
        cin >> i;
        if (i=0,1)
            cout << "Safe! Move to next round!" << endl;


You could change "i" to char, or string, but char might use a little less memory. Then instead of 0 or 1, you could accept, L R
That may be helpful later if you want to use N E S W, for direction instead.
Oops i id forget the ";" after mystring= "exit";

And I was thinking about doing that too, it would make it more simpler to program and for the person to use. I think i'll revise that section too and probably change it to char.

Thanks for the help guys. I appreciate it greatly.
Ok, so kinda adding on to this question. I made a new section of code:
1
2
3
4
5
6
7
8
9
10
11
{
int i;
            
            cout << "Left or Right?" << endl;
            cin >> i;
            if (i==0) 
                cout << "Safe! Move to next round!" << endl;
            else 
                cout << "Oh no! You fell into a ravine!" << endl;
            void exit (int exitcode);
}


I want to have it restart if you choose the wrong way. How could I go about doing this in this case? I'm thinking a do-while loop, but I don't have any idea how to code it. I also plan on having this piece of code repeated over and over. Would I need to make a do-while loop for each instance? Or is there a way to run it all in one do-while?
Topic archived. No new replies allowed.