Guess The Number – C++

What it does:
Another program which I created in labs in uni, as a task. It randomly generates a number, the user would then enter a guess and it would return either too low or too high allowing them to decide whether or not they need to increase or decrease their guess.

Code:


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

int main()
{
	srand((int)time(0));
	int randomNumber = rand()%100;
	int userGuess;
    
	do
	{
		cout << "\nPlease enter a number between 0 and 100" << endl;
		cin >> userGuess;
        
		if(userGuess>randomNumber)
		{
			cout << "Your guess is too high" << endl;
		}//if
        
		else if(userGuess<randomNumber)
		{
			cout << "Your guess is too low" << endl;
		}//else if
        
        
	}//do
	while(randomNumber!= userGuess);
    
    
	cout << "\n Your guess is correct and your number was " << userGuess << endl;
	
	
	_getch();
	return 0;
}//main

3 thoughts on “Guess The Number – C++

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s