Hey everybody my project is to make a simple guessing game. Well I've done that part but I later read that the user must be able to terminate the program if they enter 9999. I tried while(guess!=9999)but I don't know where logically to put the brackets. If there is a better way to do it please let me know. Thanks guys.
#include <iostream>
#include <cstdlib> //rand and srand functions location.
#include <ctime> //time function location.
int main ( )
{
int num = 0; //the random number to guess.
constint MAX_n = 100; //max possible number to guess (will create max random number of 100)
int guess;
int count=1;
char again;
do
{
//seed random-number generator (set up for a random number).
std::srand (time(NULL)); //time is used for the seed, since time is always changing.
//generate a random number 1-100 inclusive. Uses modulus (%) to restrict the range.
num = (std::rand() % MAX_n) +1; //the +1 takes the 0-99 rand result up to 1-100.
std::cout<<num;
std::cout << "Welcome to the guessing game!\n" << "Try and guess my number. Its between 1-100 (inclusive).";
std::cin>>guess;
for (;count<5;count++)
{
while(guess>100 || guess<1)
{
std::cout<<"Sorry your guess has to be 1 thru 100\n. Try again";
std::cin>>guess;
}
if (guess>num)
{
std::cout<<"Your guess is too high.\n Try again.";
std::cin>>guess;
}
elseif (guess<num)
{
std::cout<<"Your guess is too low.\n Try again.";
std::cin>>guess;
}
elseif (guess==num)
{
std::cout<<"Congratulations you guessed my number\n";
std::cout<<"It took you "<< count << " try/tries.\n";
break; //exit loop if correct
// <<"It took you "<< # of iterations " try/tries."
}
}//End of for with counter
std::cout<<"Would you like to try again? (Y/N)";
std::cin>>again;
}while (again=='Y'||again=='y');
return (0);
}