By Rupam Jha
In this C++ project user is asked to choose a number between 1- 10 and computer generates its own number and if number matches then user guesses correct answer else wrong.
In this we, will learn how to make number guessing game using C++.
In this game user has to choose random number between 1-10 and computer will generate its own random number between 1-10 and if user number matches with computer then user guess correct answer else wrong answer.
It helps user to think about numbers,approximations and various combinations.
#include #include using namespace std; int main() { int x,y,z=0,r=0,f=0; cout<<"********Welcome to guess number game .**********\n"<<endl; char ch; do { cout<<"Enter the guess number from 1-10 you think "; cin>>x; cout<<endl; y=rand()%10+1; if(y==x) { cout<<"Congratulations you guess correct number "<<endl; z++; } else { cout<<"Sorry,wrong guessed number Try again "<<endl; r++; } f++; cout<<"Do you want to try again y/n "; cin>>ch; cout<<endl; if(ch=='n'||ch=='y'){} else { cout<<"Invalid input.Plz enter again to play game or to exit "<<endl; } }while(ch!='n'); cout<<"You tried "<<f<<"times"<<endl; cout<<"You guessed correct number "<<z<<" times"<<endl; cout<<"You guessed wrong number "<<r<<" times"<<endl; }
OUTPUT:
********Welcome to guess number game .**********
Enter the guess number from 1-10 you think 6
Sorry,wrong guessed number Try again
Do you want to try again y/n n
You tried 1times
You guessed correct number 0 times
You guessed wrong number 1 times
EXPLANATION:
In this game we use do-while loop and user is asked to choose a number between 1-10 and if number matches to number generated by computer than he guess correct number and will be incremented and stored in z similary,wrong answers will be counted and stored in r.
It keeps the track of number of times u play game and is stored in variable f.
If you wish to continue the game say yes 'y' else no 'N'.
So this game provide workout to your brain.
Submitted by Rupam Jha (Rupamjha06)
Download packets of source code on Coders Packet
Comments