VB icon

Guess the computer number

Email
Submitted on: 1/6/2015 12:55:00 AM
By: NIHAR DINESH MANIYAR (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 389
 
     This program is a puzzle where one needs to guess a number between 1 to 100 which is generated by computer. It shows how to use rand and srand functions
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Guess the computer number
//**************************************
iostream.h
stdlib.h
time.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Guess the computer number
// Description:This program is a puzzle where one needs to guess a number between 1 to 100 which is generated by computer. It shows how to use rand and srand functions
// By: NIHAR DINESH MANIYAR (from psc cd)
//
// Inputs:The user has to guess and input a number between 1 to 100 which is generated by computer
The user is given five chances
//
// Returns:This code returns whether the user has guessed the number successfully or not
//
// Assumes:The user should know a bit of programming in C/C++
//**************************************

// This program is a puzzle where one needs to find a number between 1 to 100 which is generated by computer.
//============================================================================================================//
// This program helps us to know how to generate random numbers// 
//============================================================================================================//
// If you want to compile this code in VC++ then name this as <filename>.cpp
// If you want to compile this code in Unix then name this as <filename>.C (capital C)
// Includes
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
// Main Function
int main()
{
 
	// Prototypes
	void heading();
 
	
	int nGuessedNum; // stores computer number
	int nInputNum; // stores inputted nubmer
	int nInd;// Index
	char nGotIt='n'; // Flag to indicate Success
	char nChoice='y'; // stores user choice
 // Loop
	while(nChoice=='y')
 {
 heading(); // Displays Heading
 nGotIt=' '; 
 
		
		// Initailize a seed for random number generation using current time 
		srand(static_cast<unsigned>(time(NULL)));
		
		// Increases randomness to get effective random numbers
		for(nInd=1;nInd<=10;nInd++)
			rand();
 
		// Generate Computer number
		// formala : lower_range + (int) ( 1 + (lower_range + upper_range) * rand() / (RAND_MAX + 1.0));
		// here 1.0 is used to get a double value, so that the random number varies
 nGuessedNum=1 + (int) (100 * rand()/(RAND_MAX + 1.0));
		
		
 // This loop allows user to get the computer number in 5 chances
		for(nInd=1;nInd<6;nInd++)
 {
cout<<"\n\n\t\tThis is Your Chance No = "<<nInd;
cout<<"\n\t\tEnter the number between 1 to 100 :-";
cin>>nInputNum;
			// if your number is greater than computer's number 
			if(nInputNum>nGuessedNum)
cout<<"\n\t\tYour Number is Greater than Computer Number";
// if your number is lesser than computer's number 
			else if(nInputNum<nGuessedNum)
cout<<"\n\t\tYour Number is Lesser than Computer Number";
// if your number is equal to computer's number
			else
{
cout<<"\n\t\tHip Hip Hurray.... You have guessed the correct number";
nGotIt='y';
break;
}
 }
 
		// if user cannot guess the computer's number in 5 chances then
		if(nGotIt!='y')
		{
			cout<<"\n\t\tOpps, Your chances are over ";
			cout<<"\n\t\tThe computer number is = "<<nGuessedNum;
		}
 
		// Do u want to continue
		cout<<"\n\n\t\tDo you want to play again [y/n] ?";
 cin>>nChoice;
 }
	return 0; 
}
// Heading which will displayed
void heading()
{
 cout<<"\t\tWelcome to the Game of Guessing....";
 cout<<"\n\n\t\tThe computer has guessed a number between 1 to 100";
 cout<<"\n\t\tYou have to the find the number. And you have 5 chances..";
 cout<<"\n\t\tGet Set Go ...";
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.