As many times as the user wants, play a guessing game with the user. The program randomly selects a secret number in the range [1,100], then lets the user guess until he gets the number. When the user is wrong, tell him or her whether he or she is too high or too low. When the user guesses the right answer, ask whether the user wants another game by inputting 'y' or 'n'. For each game, the program selects another random number as the answer. An example run of your program might go as
Guess the secret number:
7
No, too low
10
No, too low
20
No, too low
30
No, too low
42
That's right! Want to play again (y/n)? y
Guess the secret number:
84
That's right! Want to play again (y/n)? y
Guess the secret number:
2
No, too high
87
No, too high
1
That's right! Want to play again (y/n)? n
Ok, bye
#include <iostream>
#include <iomanip>
using namespace std;
char chr;
int main()
{
int number;
int guess;
int tries;
char answer;
srand(number>0);
do
{
number=rand()%100+1;;
cout<<"Enter a number between 1 and 100"<<endl;
cin>>guess;
if (number<guess)
cout<<"Too high try again"<<endl;
tries=1;
} while(number>guess);
cout<<"Too low try again"<<endl;
tries++;
if(number==guess)
cout<<"Congratualtions!! "<<endl;
cout<<"You got the right number in "<<tries<<" tries";
do
{
cout<<"Would you like to play again? Enter Y/N";
cin>>answer;
if ('N')
cout<<"Thanks for playing!";
} while(answer='Y');
@ lechuga2000, You are correct. It is why I said to use those headers #include <cstdlib> //For rand and srand and #include <ctime> //For the time function and not the #include <stdlib.h> nor #include <time.h>
According to the ISO Standard for C++,
"The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible with C."
That being said, if the compiler is old and not in accordance with the ISO Standard for C++, then that it is another issue.