help noob modify c++ game

Write your question here.I'm a noob, I wrote this code to play a little game with random numbers, but the code doesn't run properly, I want computer to be able to tell the player if player guess is low,high or correct, and I need the game to keep promoting the player to input an answer til the player wins

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
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int coinflip(int x, int y){ // function that          //                             compares the //      //                             players guess to //                       computer random number                                                         

return rand() % (y - x);
	
}
bool playgame (int gameon){  // function that           //                  allows the player to either //                    play or quit game
	if (gameon== 1){
		return true;
		return false;
	}
}
int main(){ 
            srand(time(NULL));
            int userinput;
            string userinput2;
            int userinput3;
            int x=1;
            int y=7;
            cout <<"welcome to the game" << endl<<endl<<endl;
            cout << "to play press 1 " << endl;
            cout << " to exit press 2 " << endl;
            cin >> userinput ;
            if (userinput!=1){
            	cout << " thanks for playing " <<endl; return 0;
            }
            if (playgame(userinput) == true){
            	cout << "computer is thinking of a number, to guess what number it is enter < yes>" << endl;
            	cin >> userinput2;
            	if (userinput2 == "yes"){
            		cout << " guess the number computer is thinking and enter it below, numbers ranging from 1-6 " << endl;
            	  	cin >> userinput3 ;
			}
				  
            		
            		if (userinput3==coinflip(x,y)){
            			cout << " congrats you win" << endl;}
            		
            		if (userinput3<coinflip(x,y)){
            			cout << " too low " << endl;}
            		
            		if (userinput3>coinflip(x,y)){
            			cout << " too high " << endl;}
						} 
						           
            }
            	
            
to start with

1
2
3
4
5
6
bool playgame (int gameon){  
	if (gameon== 1){
		return true;
		return false;
	}
}


that will never return false the right way to do it would be:

1
2
3
4
5
6
bool playgame (int gameon){ 
	if (gameon== 1){
		return true;
	}
return false;
}



and as for the other problem the simple structure of that would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (/*players guess isn't equal to number*/)
{
  if (/*players number < computers number*/)
{
     cout << "The number is larger than your guess" << endl;
}

else if (/*players number > computers number*/)
{
     cout << "The number is smaller than your guess" << endl;
}

else if (/*players number == computers number*/)
{
     break;
}
}
cout << "YOU WIN" << endl;



(i didn't test the code but it should work)
Last edited on
Thanks Bro, will test the modified code!
Topic archived. No new replies allowed.