I am to to write a program that generates a number from 1-9 & allow the user to guess the number. I have two questions. 1) Why am I getting the same number every game? 2) Why when the guess isnt correct does all my cout's re appear? I had this happen b4 in a C program & trapped the menu from showing again. This is why I created a char c. Not working this time. If anyone can point me in the right direction, I appreciate it.
Code:
#include<ctime> //rand() uses same number; srand uses different number for new game
#include<iostream>
#include<cstdlib> //required to use rand() function
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int number;
number = rand()%10;
int guess;
char c; //used to trap extra menu
do
{
cout<<"Let's play a game!"<<endl;
cout<<"I'm thinking of a number from 1 to 9"<<endl;
cout<<"Let's see if you can guess my number!"<<endl;
cout<<"What number am I thinking? "<<endl;
cin>>guess;
cout<<c<<endl;
if (guess == number)
{
cout<<"Lucky guess!!"<<endl;
}
else if ((guess-2 == number) || (guess+2 == number))
{
cout<<"Oh, so close! You are within two of the number."<<endl;
cout<<"Try again! "<<endl;
cout<<c<<endl;
}
else
{
cout<<"Not even close! Your are more than two off the number."<<endl;
cout<<"Try again! "<<endl;
cout<<c<<endl;
}
}while(guess != number);
system("PAUSE"); //prevents console window from closing
return 0;
}