#WSQ03

Pick a number!

This is it, the third program. This program needs a loop, in case the user wants to keep playing. You basically you just create it with a “do” and a “while”.  To generate the random number you have to include two libraries: “ctime” and “cstdlib”. I included some few modifications to the program Ken asked. If you need any help, just ask for it!

Code:

#include
#include
#include

using namespace std;

int main()
{
int guess, num, cont ;
string res;
cout<<endl;
cout<<“Try to guess the number I have in mind (between 1 and 100).”<<endl;
do {
srand(time(NULL));
num=1+rand()%(101);
cont=4;
cout<<“You have 5 oportunities to guess.”<<endl;
cout<<“Please insert your guess:”<<endl; cin>>guess;
while(num!=guess && cont>0)
{
cont=cont-1;
if(guess>num)
{
cout<<“The number “<<guess<<” is too high, please try again.”<<endl; cin>>guess;
}
else
{
cout<<“The number “<<guess<<” is too low, please try again.”<<endl; cin>>guess;
}
}
if(guess!=num)
{
cout<<“Good luck next time my friend, the number was “<<num<<“.”<<endl;
}
else
{
cout<<“Congratulations! You guessed the number in just “<<5-cont<<” attempts.”;
}
cout<<“You want to try your luck again? Yes/No.”<<endl; cin>>res;
}
while(res!=”no” && res!=”NO” && res!=”No”);
return 0;
}

wsq03

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s