i need help quickly...about if statements

i need to write program which,at the beginning program generates radom number in the range specified by the user(ask the user for the smallest and the biggest value).the user try to guess unknown number.program answers"to small"or "to big" if the user is wrong and if the user is succesfull,the program ask,whether the user want to play again.


thankss :)
where is your attempt?
This sort of thing is what you want:

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
36
37
38
39
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int min, max, random, guess; //Variables we need
    cout << "Minimum?\n";
    cin >> min;
    cout << "Maximum?\n";
    cin >> max; //Ask the user for the minimum and maximum numbers
    random = (rand() % (max - min)) + min; //Make our random number
    cout << "Have a guess\n";
    while(guess != random)
    {
        cin >> guess; //Get their guess
        if(guess > random) //If its too big...
        {
            cout << "Too big!\n";
            bool tryagain;
            cout << "Try again?\n"; //Ask them if they want to try again
            cin >> tryagain;
            if(!tryagain)
                return 0; //No? Then end the program, otherwise continue the loop
        }
        if(guess < random) //If its too small...
        {
            cout << "Too small!\n";
            bool tryagain;
            cout << "Try again?\n";  //Ask them if they want to try again
            cin >> tryagain;
            if(!tryagain)
                return 0; //No? Then end the program, otherwise continue the loop
        }
    }
    cout << "Well done!"; /*The only time this will ever be reached 
is if the loop ends without the program ending, 
which can only happen when the
guess is equal the the random number*/
}
Last edited on
thanks a lot .... it ll help me much :)
Topic archived. No new replies allowed.