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>
usingnamespace 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;
}
elseif(guess>number)
{
-- tries;
cout<<"Too high, you have " << tries << " more guess(es)" << endl;
cin>>guess;
}
elseif(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