game program need fixing

Write your question here.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{

    char choice;
    int number;
    int RandomNumber;
    int RandomMax=6;
    int money=500;

    srand (time(NULL));


        cout<<"Game"<<endl;
        cout<<"Hello human, I am the computer. the game we are playing is called Avoid."<<endl;
        cout<<"here are the game rules:"<<endl;
        cout<<"1. pick a number between 1 and 6"<<endl;
        cout<<"2. the computer will guess your number"<<endl;
        cout<<"3. if computer guess incorrectly you win twice amount of money you have"<<endl;
        cout<<"4. if computer guess correctly you lose halve your money"<<endl;
        cout<<"5. after each round the maximun range drops by one"<<endl;
        cout<<"6. in the final round when the only choice is 1 or 2. if you win, you win"<<endl;
        cout<<"   five time the amount of money however if lose all of your money and lose the game"<<endl;
        cout<<"want to play(Y/N)"<<endl;
        cin>>choice;

        choice=toupper(choice);

        do
        {

        if(choice=='y' || choice=='Y')
        {




        cout<<"select a number between 1 - "<<RandomMax<<endl;
        cin>>number;

        if(number<=RandomMax && number>=1)
        {


            RandomNumber=(rand()%(RandomMax-1+1))+1;


            if(number != RandomNumber && number<=RandomMax && number>=1 && choice=='Y')
            {

                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;

                RandomMax--;
                money=money*2;

                cout<<"you have $"<<money<<endl;

                cout<<"You survice this round Human."<<endl;


            }

            if(number == RandomNumber && number<=RandomMax && number>=1  && choice=='Y' )
            {
                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;
                cout<<"you lost human."<<endl;


                RandomMax--;
                money=money/2;

                cout<<"you have $"<<money<<endl;



                money=money+500;
            }




        }


        else
        {

            cout<<"invalid input"<<endl;
            cout<<"want to play"<<endl;
            cin>>choice;



        }

        while(RandomMax==2)
        {
            cout<<"This is the final round. Win and you win five times the money you have."<<endl;
            cout<<"select a number between 1 - "<<RandomMax<<endl;
            cin>>number;

            RandomNumber=(rand()%(RandomMax-1+1))+1;

            if(number != RandomNumber && number<=RandomMax && number>=1)
            {
                cout<<"you have beaten me and heres your prize"<<endl;

                money=money*5;

                cout<<"you won $"<<money<<endl;
                cout<<"do you want to play again"<<endl;
                cin>>choice;

                RandomMax=6;

            }

            if(number == RandomNumber && number<=RandomMax && number>=1)
            {
                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;
                cout<<"You lost everything Human."<<endl;

                 RandomMax=6;
                money=money-money;

                cout<<"you have $"<<money<<endl;
                cout<<"Want to play  again"<<endl;
                cin>>choice;
                money=money+500;
            }

            else
            {
                cout<<"invalid input"<<endl;
                cout<<"want to continue playing"<<endl;
                cin>>choice;


            }

        }





        }

        if(choice=='n' || choice=='N')
        {
            cout<<"Maybe next time"<<endl;
        }

        else
        {
            cout<<"invalid input"<<endl;
            cout<<"Want to play the game(Y/N)"<<endl;
            cin>>choice;
        }

}while(choice=='y'|| choice=='Y');



    return 0;
}


need a little help fixing this program.
when i run this program i get this

Game
Hello human, I am the computer. the game we are playing is called Avoid.
here are the game rules:
1. pick a number between 1 and 6
2. the computer will guess your number
3. if computer guess incorrectly you win twice amount of money you have
4. if computer guess correctly you lose halve your money
5. after each round the maximun range drops by one
6. in the final round when the only choice is 1 or 2. if you win, you win
five time the amount of money however if lose all of your money and lose the game
want to play(Y/N)
input: y
select a number between 1 - 6
input:5
Computer :4
Human :5
you have $1000
You survice this round Human.
invalid input
Want to play the game(Y/N)

the "invalid input" on line 166 runs even though the input wasn't invalid

Last edited on
if you make line 159 else if(choice=='n' || choice=='N') it will fix the problem, but there is still a logic problem. In case you don't catch it, here is an example output after only fixing the problem on line 159. You can lose and win. This should get you going though.


Game
Hello human, I am the computer. the game we are playing is called Avoid.
here are the game rules:
1. pick a number between 1 and 6
2. the computer will guess your number
3. if computer guess incorrectly you win twice amount of money you have
4. if computer guess correctly you lose halve your money
5. after each round the maximun range drops by one
6. in the final round when the only choice is 1 or 2. if you win, you win
five time the amount of money however if lose all of your money and lose the game
want to play(Y/N)
y
select a number between 1 - 6
5
Computer :1
Human :5
you have $1000
You survice this round Human.
select a number between 1 - 5
1
Computer :3
Human :1
you have $2000
You survice this round Human.
select a number between 1 - 4
2
Computer :3
Human :2
you have $4000
You survice this round Human.
select a number between 1 - 3
1
Computer :1
Human :1
you lost human.
you have $2000
This is the final round. Win and you win five times the money you have.
select a number between 1 - 2
1
you have beaten me and heres your prize
you won $12500
do you want to play again
thank you for the help
I fix it but and added a few things but i have new issues now.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{

    char choice;
    int number;
    int RandomNumber;
    int RandomMax=6;
    int money=500;

    srand (time(NULL));


        cout<<"Game"<<endl;
        cout<<"Hello human, I am the computer. the game we are playing is called Avoid."<<endl;
        cout<<"here are the game rules:"<<endl;
        cout<<"1. pick a number between 1 and 6"<<endl;
        cout<<"2. the computer will guess your number"<<endl;
        cout<<"3. if computer guess incorrectly you win twice amount of money you have"<<endl;
        cout<<"4. if computer guess correctly you lose halve your money"<<endl;
        cout<<"5. after each round the maximun range drops by one"<<endl;
        cout<<"6. in the final round when the only choice is 1 or 2. if you win, you win"<<endl;
        cout<<"   five time the amount of money however if lose all of your money and lose the game"<<endl;
        cout<<"want to play(Y/N)"<<endl;
        cin>>choice;

        choice=toupper(choice);

        do
        {

        if(choice=='y' || choice=='Y')
        {




        cout<<"select a number between 1 - "<<RandomMax<<endl;
        cin>>number;

        if(number<=RandomMax && number>=1)
        {


            RandomNumber=(rand()%(RandomMax-1+1))+1;


            if(number != RandomNumber && number<=RandomMax && number>=1 && choice=='Y')
            {

                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;

                RandomMax--;
                money=money*2;

                cout<<"you have $"<<money<<endl;

                cout<<"You survice this round Human."<<endl;


            }

            if(number == RandomNumber && number<=RandomMax && number>=1  && choice=='Y' )
            {
                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;
                cout<<"you lost this round human."<<endl;


                RandomMax--;
                money=money/2;

                cout<<"you have $"<<money<<endl;



                money=money+500;
            }




        }


        else
        {

            cout<<"invalid input"<<endl;
            cout<<"want to play"<<endl;
            cin>>choice;



        }

        while(RandomMax==2)
        {
            cout<<"This is the final round. Win and you win five times the money you have."<<endl;
            cout<<"select a number between 1 - "<<RandomMax<<endl;
            cin>>number;

            RandomNumber=(rand()%(RandomMax-1+1))+1;

            if(number != RandomNumber && number<=RandomMax && number>=1)
            {
                cout<<"you have beaten me and heres your prize"<<endl;

                money=money*5;

                cout<<"you won $"<<money<<endl;
                cout<<"do you want to play again"<<endl;
                cin>>choice;

                RandomMax=6;

            }

            if(number == RandomNumber && number<=RandomMax && number>=1)
            {
                cout<<"Computer :"<<RandomNumber<<endl;
                cout<<"Human :"<<number<<endl;
                cout<<"You lost everything Human."<<endl;

                 RandomMax=6;
                money=money-money;

                cout<<"you have $"<<money<<endl;
                cout<<"Want to play  again"<<endl;
                cin>>choice;
                money=money+500;
            }

            else
            {
                cout<<"invalid input"<<endl;
                cout<<"want to continue playing"<<endl;
                cin>>choice;


            }

        }



        }

        else if(choice=='n' || choice=='N')
        {
            cout<<"Maybe next time"<<endl;
        }

        else
        {
            cout<<"invalid input"<<endl;
            cout<<"Want to play the game(Y/N)"<<endl;
            cin>>choice;
        }

        cout<<"do you want to continue"<<endl;
        cin>>choice;

}while(choice=='y'|| choice=='Y');



    return 0;
}


now it does this

1. pick a number between 1 and 6
2. the computer will guess your number
3. if computer guess incorrectly you win twice amount of money you have
4. if computer guess correctly you lose halve your money
5. after each round the maximun range drops by one
6. in the final round when the only choice is 1 or 2. if you win, you win
five time the amount of money however if lose all of your money and lose the game
want to play(Y/N)
y
select a number between 1 - 6
5
Computer :2
Human :5
you have $1000
You survice this round Human.
do you want to continue
y
select a number between 1 - 5
4
do you want to continue
y
select a number between 1 - 5

it repeats "do you want to continue" "select a number between 1-5"
please help
Last edited on
that's because of line 55
if(number != RandomNumber && number<=RandomMax && number>=1 && choice=='Y')

you are inputting a lower case y and thus this test fails, it only checks for a big Y. You only need the first condition anyway, so just get rid of the last 3. Change line 55 to

if(number != RandomNumber )

and do the same for you other if statements. While I'm at it, take a look at line 78, there's a problem there too as alluded to in my first post.
Topic archived. No new replies allowed.