Only picks choice 1?

In my code, everything I choose for the first pick goes directly to choice = 1.
How do I make it so that if you type 2, it goes to choice 2?




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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int choice, Prounds, ap;
    Prounds = 5;
    ap = 1;
    
    
    cout<<"This is a atbg (adventure text based game) and at each situation,\n";
    cout<<"you will be presented with four choices.  Type in the number of\n";
    cout<<"the choice you choose.\n\n\n";
                cout<<"You wake up inside of a tent. You look around,\n";
                cout<<"and find a pistol with 5 shots in it, a piece of paper,\n";
                cout<<"and a strange green device with a big Red button on it.\n";
                cout<<"What do you do?\n\n";
                cout<<"1. Look outside the tent\n";
                cout<<"2. Press the big red button.\n";
                cout<<"3. Study the gun.\n";
                cout<<"4. Study the paper.\n";
                cin>>choice;
                if (choice=1)
                {
                        cout<<"You look outside, to learn that a blizzard is\n";
                        cout<<"Raging and it is too blurry to make out anything.\n";
                        cout<<"You quickly withdraw your head.\n";
                        cout<<"Now what do you do?\n";
                        cout<<"1. Press the big red button.\n";
                        cout<<"2. Study the gun.\n";
                        cout<<"3. Study the paper.\n";
                        cin>>choice;
                        if (choice = 1)
                        {
                                   cout<<"Lightning strikes the tent and you die!";
                                   cout<<"GAME OVER...";
                                   return 0;
                                   }
                        if (choice = 2)
                        {
                                   cout<<"The gun is a 9 mm gun with 5 bullets in the mag.\n";
                                   cout<<"What do you do?\n";
                                   cout<<"1.Go to sleep\n";
                                   cout<<"2.Load the gun and bum rush outside.\n";
                                   cout<<"3.Commit suicide\n";
                                   cin>>choice;
                                   
                                   
                                   }
                        if (choice = 3)
                        {
                        
                }                  }
                           
                           
                           
                if (choice = 2)
                {
                 cout<<"Lighting strikes the tent and you die.\n";
                 cout<<"GAME OVER...";
                 return 0;
                           }
                if (choice = 3)
                {
                           cout<<"The gun is a 9 mm gun with 5 bullets in the mag.\n";
                           cout<<"Suddenly, the gun fires and kills you.\n";
                           cout<<"GAME OVER";
                           return 0;
                           }
                if (choice = 4)
                {
                           cout<<"The paper reads: You will die.\n";
                           cout<<"A polar bear attacks the tent and you die.\n";
                           cout<<"GAME OVER\n";
                           return 0;
                           }
                else
                {
                    cout<<"Game over, you neglected to follow teh rules.";
                    return 0;
                }
                
return 0;
}
Last edited on
2 things: First, codeblocks, [code_] code goes here [/code_] without the underscore

second, it should be if(choice == 1), otherwise you are assigning the value 1 to choice, and returning the value 1, which evaluates to true.
In addition you might want to consider using a switch case statement structure for your conditions, other wise use else if statements... example
1
2
3
4
5
6
7
8
9
if (x != 5){
      // do stuff
}
else if (x == 5 ){
     // do stuff
}
else{
     // default condition
}
Now, after a return 0; it just closes. How can I make it so the reader can actually read the text as in

1
2
3
4
cout<<"\n\nThe gun is a 9 mm gun with 5 bullets in the mag.\n";
cout<<"Suddenly, the gun fires and kills you.\n";
cout<<"GAME OVER";
return 0;


It doesnt show anything, it just closes.
cin.get() will stop the screen until they hit enter.
flclempire wrote:
cin.get() will stop the screen until they hit enter.

Actually I believe it waits until the user presses the mythical "any" key.

Also you may (really you don't need to always do this, but it never hurts) want to call cin.sync() just in case one of those "any" keys are waiting in the buffer.

http://cplusplus.com/forum/beginner/1988
Last edited on
Here is your fixed code, I edited only the "==" and the Else if... oh and you left the # in include cstdlib

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int choice, Prounds, ap;
    Prounds = 5;
    ap = 1;
    
    
    cout<<"This is a atbg (adventure text based game) and at each situation,\n";
    cout<<"you will be presented with four choices.  Type in the number of\n";
    cout<<"the choice you choose.\n\n\n";
                cout<<"You wake up inside of a tent. You look around,\n";
                cout<<"and find a pistol with 5 shots in it, a piece of paper,\n";
                cout<<"and a strange green device with a big Red button on it.\n";
                cout<<"What do you do?\n\n";
                cout<<"1. Look outside the tent\n";
                cout<<"2. Press the big red button.\n";
                cout<<"3. Study the gun.\n";
                cout<<"4. Study the paper.\n";
                cin>>choice;
                if (choice == 1) //ALWAYS when doing IFs, and some of the loops you need to to double = :D
                {
                        cout<<"You look outside, to learn that a blizzard is\n";
                        cout<<"Raging and it is too blurry to make out anything.\n";
                        cout<<"You quickly withdraw your head.\n";
                        cout<<"Now what do you do?\n";
                        cout<<"1. Press the big red button.\n";
                        cout<<"2. Study the gun.\n";
                        cout<<"3. Study the paper.\n";
                        cin>>choice;
                        if (choice == 1)
                        {
                                   cout<<"Lightning strikes the tent and you die!\n";
                                   cout<<"GAME OVER...";
                                   return 0;
                                   }
                        else if (choice == 2)
                        {
                                   cout<<"The gun is a 9 mm gun with 5 bullets in the mag.\n";
                                   cout<<"What do you do?\n";
                                   cout<<"1.Go to sleep\n";
                                   cout<<"2.Load the gun and bum rush outside.\n";
                                   cout<<"3.Commit suicide\n";
                                   cin>>choice;
                                   
                                   
                                   }
                        else if (choice == 3)
                        {
                        
                }                  }
                           
                           
                           
                if (choice == 2)
                {
                 cout<<"Lighting strikes the tent and you die.\n"; // You needed another \n here...
                 cout<<"GAME OVER...";
                 return 0;
                           }
                else if (choice == 3)
                {
                           cout<<"The gun is a 9 mm gun with 5 bullets in the mag.\n";
                           cout<<"Suddenly, the gun fires and kills you.\n";
                           cout<<"GAME OVER";
                           return 0;
                           }
                else if (choice == 4)
                {
                           cout<<"The paper reads: You will die.\n";
                           cout<<"A polar bear attacks the tent and you die.\n";
                           cout<<"GAME OVER\n";
                           return 0;
                           }
                else
                {
                    cout<<"Game over, you neglected to follow teh rules.";
                    return 0;
                }
                
return 0;
}
I typed in cin.ignore(); but it shows the text, then quickly shuts down. Also, cin.get(); doesnt work at all.
Last edited on
closed account (D80DSL3A)
Please see the thread titled "Console closing down" near the top of this forum page. It concerns your problem. It's a sticky thread because the problem appears so frequently in this forum.
I found out what will work,
1
2
3
cin.ignore();
cin.ignore();
return 0;
Topic archived. No new replies allowed.