how to do? help

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]
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace 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;
else if (guess > numPicked)
cout << "\nYou should try higher values." << endl;

}
cout << " Congratulations!!" << endl;
cout << "It took you" << guessNum<<"guesses." << endl;

return 0;
Last edited on
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
If I understand your correctly, you want 54 to be the correct number. And then you want to close the program by pressing the enter key?
yes yes,when i enter 54,it will be writen "coungrutulations" and when i enter secondly it will close,thx
I've already given you a template, see if you can model your program after that.

I'm not going to write the whole thing for you. Keep trying.
i will try.
Topic archived. No new replies allowed.