help with my program

Here is the program i have been working on, it is meant to pick a number between 1-100 and have the user guess the number in 7 try's. Also If the user enters a number out of the range of 1 to 100 then you must print a message to the user telling them that the number is out of range and allow them to guess again without decrementing the guesses left count.
Any help would be greatly appreciated.


#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand( (unsigned)time( NULL ) );
int number=rand()%100+1;
int tries = 7;
int guess;
char again;

do
{


cout<<"The computer has selected a number between 1-100. Try to guess the number! you have 7 guesses avalible. " << endl;
cin>>guess;


if(guess=number)
{
cout<<"Congrats!! You got it with " << tries << " guess(es) remaining! " << endl;

}


else if(guess>number)
{
-- tries;
cout<<"Too high, you have " << tries << " more guess(es)" << endl;
cin>>guess;
}
else if(guess<number)
{
-- tries;
cout<<"Too low, you have " << tries << " more guess(es)" << endl;
cin>>guess;
}

if (tries = 0)
{
cout << "I'm sorry, you lose! \n";
cout << "The answer is: " << number << endl;

}

cout << "Would you like to go again? y/n ";
cin >> again;
}


while (again == 'y' || again == 'Y');

system("PAUSE");
return 0;
}

the wrong result you get is from your conditions )))))

if(guess=number)

if (tries = 0)

you did attribution :) ...... chnage them to == condition
here is the updated code, it still is not working properly.

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
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
	srand( (unsigned)time( NULL ) );
	int number=rand()%100+1;
	int tries = 7;
	int guess;
	char again;

	do
		{
		

		cout<<"The computer has selected a number between 1-100. Try to guess the number! you have 7 guesses avalible.  " << endl;
		cin>>guess;


		if(guess == number)
			{
			cout<<"Congrats!! You got it with " << tries << " guess(es) remaining! " << endl;
		 
			}


		else if(guess>number)
			{
			-- tries;
			cout<<"Too high, you have " << tries << " more guess(es)" << endl;
			cin>>guess;
			}
		else if(guess<number)
			{
			-- tries;
			cout<<"Too low, you have " << tries << " more guess(es)" << endl;
			cin>>guess;
			}

		if (tries == 0) 
			{
			cout << "I'm sorry, you lose! \n";
			cout << "The answer is: " << number << endl;
	
			}

		cout << "Would you like to go again? y/n ";
		cin >> again;
		}


while (again == 'y' || again == 'Y');

system("PAUSE");
return 0;
}
you have other mistakes....look...you have this piece of code


else if(guess>number)
{
-- tries;
cout<<"Too high, you have " << tries << " more guess(es)" << endl;
cin>>guess;
}
....when you input guess it never tests

if(guess == number) and
if(guess>number)
conditions for your input guess ....

and it realtes to your other conditions...

also you must write your

cout << "Would you like to go again? y/n ";
cin >> again;

in the body of if(tries==0) construction.... but in that case your condition inside the while isn't good....
i recommend you to do some changes on your program....transfer your while condition also in the body of
if(tries==0) condition and when

again == 'y' || again == 'Y' condition is wrong, do break...

let the while condition empty .... and also ignore your cin>>guess-s in conditions....
expeience that
Last edited on
OK, i have fixed the code, i still need to prevent the user from entering a number outside of 1-100, i tried this :
1
2
3
4
5
if (number != 1-100)
			{
			cout<<" Please pick a number from 1-100" << endl;
                        cin>>guess;
			}

but it didn't work. what is the correct command ?
i understand your logic but compiler understands your 1-100 as -99 :
your condition must be like this

if ( number<1 || number>100)

the || symbol means or !!
Last edited on
thanks for your help it worked great !
you are quite welcome :)
Topic archived. No new replies allowed.