Write a program in which the user is supposed to guess the number hidden in
the code. ( this will be an integer variable called “guess” and can be initialized
to any value between 0 and 100). If the number entered by the user is larger
than the hidden number, you should print, “You should try smaller values”.
Likewise if the number entered by the user is smaller than the hidden number,
you should print “You should try larger values”. If user enters the correct
number, you should print “Congratulations !!!” and exit the program. You
should print “Invalid, guess should be between 0 and 100” for guesses other
than [0-100]
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main (void){
cout << "Welcome to the number guessing game." << endl;
cout << endl;
cout << " I have picked a number between 1-100. \n \n";
srand(time(0));
// stores the random number
int numPicked = rand() % 100 + 1;
int guess = 0; // stores the number the user guessed
int guessNum; // stores the number of guesses
for (guessNum = 0; guess != numPicked; guessNum++)
{
cout << "What number would you like to guess?" << endl;
cin >> guess;
if (guess < numPicked)
cout << "\nYou should try smaller values." << endl;
elseif (guess > numPicked)
cout << "\nYou should try higher values." << endl;
}
cout << " Congratulations!!" << endl;
cout << "It took you" << guessNum<<"guesses." << endl;
return 0;
thnx,something like that but number will be 54 that we search.and when i push enter 54,it will write congrutulate,and again i push enter,it will close......please help me,i am beginner in this programme