#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
/*
This program plays a simple game.The computer picks a random number
from
0 to 100, and the user tries to guess the number.
*/
const int totchan=7;
void main()
{
int number; //the computer's random number
int guess; //the user's guess
//chanscor stores score for 1successful chance.
int chances=0,score=0,chanscor;
char ans;
do
{ clrscr();
chances=score=0;
cout<<"\nWelcome to the High/Low game.";
cout<<"I will pick a random number from 0 to 100."<<endl;
cout<<"You must try to guess the number."<<endl;
randomize();
number=(int)(rand()%100);
chanscor=100/totchan; //score for each successful chance
do
{
cout<<"What is your guess? (0 to 100) "<<endl;
cin>>guess;
if((guess<0)||(guess>100))
{
cout<<"Sorry, but your guess "<<guess<<"must be from 0 to 100."<<endl;
}
else if(guess < number)
{
cout<<guess<<" is low.Try a higher number."<<endl;
}
else if(guess > number)
{
cout<<guess<<" is high.Try a lower number."<<endl;
}
else
{
//if correct number is guessed
//number is correct, and the "do" loop will end below
cout<<guess<<" is correct. Congratulations!"<<endl;
score=chanscor*(totchan-chances); //score calculated for number ofchances left
cout<<"Your score is "<<score<<endl;
break;
}
chances++;
if(guess!=number)
cout<<"Now you have "<<totchan-chances<<"chances left."<<endl;
if(chances==totchan)
{ cout<<"Only "<<totchan<<"chances are allowed.Better luck nexttime";
cout<<"The actual number was "<<number<<endl;
break;
}
}while (guess!=number);
cout<<"Thank you for playing High/Low!"<<endl;
cout<<"Want to play it again? (y/n)..."<<endl;
cin>>ans;
}while(ans=='y' || ans=='Y');
}
You must try to guess the number.
What is your guess? (0 to 100)
56
56 is high.Try a lower number.
Now you have 6chances left.
What is your guess? (0 to 100)
28
28 is low.Try a higher number.
Now you have 5chances left.
What is your guess? (0 to 100)
36
36 is low.Try a higher number.
Now you have 4chances left.
What is your guess? (0 to 100)
46
46 is high.Try a lower number.
Now you have 3chances left.
What is your guess? (0 to 100)
40
40 is correct. Congratulations!
Your score is 42
Thank you for playing High/Low!
Want to play it again? (y/n)...
#include<conio.h>
#include<stdlib.h>
/*
This program plays a simple game.The computer picks a random number
from
0 to 100, and the user tries to guess the number.
*/
const int totchan=7;
void main()
{
int number; //the computer's random number
int guess; //the user's guess
//chanscor stores score for 1successful chance.
int chances=0,score=0,chanscor;
char ans;
do
{ clrscr();
chances=score=0;
cout<<"\nWelcome to the High/Low game.";
cout<<"I will pick a random number from 0 to 100."<<endl;
cout<<"You must try to guess the number."<<endl;
randomize();
number=(int)(rand()%100);
chanscor=100/totchan; //score for each successful chance
do
{
cout<<"What is your guess? (0 to 100) "<<endl;
cin>>guess;
if((guess<0)||(guess>100))
{
cout<<"Sorry, but your guess "<<guess<<"must be from 0 to 100."<<endl;
}
else if(guess < number)
{
cout<<guess<<" is low.Try a higher number."<<endl;
}
else if(guess > number)
{
cout<<guess<<" is high.Try a lower number."<<endl;
}
else
{
//if correct number is guessed
//number is correct, and the "do" loop will end below
cout<<guess<<" is correct. Congratulations!"<<endl;
score=chanscor*(totchan-chances); //score calculated for number ofchances left
cout<<"Your score is "<<score<<endl;
break;
}
chances++;
if(guess!=number)
cout<<"Now you have "<<totchan-chances<<"chances left."<<endl;
if(chances==totchan)
{ cout<<"Only "<<totchan<<"chances are allowed.Better luck nexttime";
cout<<"The actual number was "<<number<<endl;
break;
}
}while (guess!=number);
cout<<"Thank you for playing High/Low!"<<endl;
cout<<"Want to play it again? (y/n)..."<<endl;
cin>>ans;
}while(ans=='y' || ans=='Y');
}
Sample Output :
Welcome to the High/Low game.I will pick a
random number from 0 to 100.You must try to guess the number.
What is your guess? (0 to 100)
56
56 is high.Try a lower number.
Now you have 6chances left.
What is your guess? (0 to 100)
28
28 is low.Try a higher number.
Now you have 5chances left.
What is your guess? (0 to 100)
36
36 is low.Try a higher number.
Now you have 4chances left.
What is your guess? (0 to 100)
46
46 is high.Try a lower number.
Now you have 3chances left.
What is your guess? (0 to 100)
40
40 is correct. Congratulations!
Your score is 42
Thank you for playing High/Low!
Want to play it again? (y/n)...
No comments:
Post a Comment