HELP PLZguess the number (random+functions)

write a program that picks a number between 1 and 100, and then lets the user guess what the number is.the program should tell the user if their guess is too high, too low, or just right.

so the problem is that i cant compare the input with the outcome (cause its in another function), any ideas on how to make that happen?(without major changes to the code)
Thanks.



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;

int randRange (int lowest,int highest)
{
while (1)
    {
        int rand_result = rand();
        if (rand_result >= lowest && rand_result <= highest)
        {
            return rand_result;

        }

    }

}

int main()
{
    int input;
    srand(time(NULL));
    cout << randRange(0, 100)<<endl;
    cout << "I picked a number from 1 to 100, guess which one: "<<endl;
    cin >> input;
    while (1)
{
    if (input < ???????)
    {
        cout << "too low"<<endl;
        cin >> input;

    }
    else if (input > ???????)
    {
        cout << "too high"<<endl;
        cin >> input;
    }
    else if (input == ???????)
    {
        cout << "right"<<endl;
        break;
    }
}

std::cin.clear(); // reset any error flags
std::cin.ignore(32767, '\n'); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user
return 0;
}




Last edited on
closed account (48T7M4Gy)
In main

1
2
3
4
5
6
7
int output = randRange( 0,100);
...
if ( input > output) etc
...
if (input < output) etc
...
if (input == output) etc
[/code]
Last edited on
the input is the user's guess
the outcome is the random number
the other function is randRange

sorry about that, though i thought it was kinda obvious, my bad anyway
closed account (48T7M4Gy)
Well done with code tags - please use in future. See above :)
i had already tried something like that before, it doesnt work, did you try and it worked?
closed account (48T7M4Gy)
No, I didnt try it. If randRange is returning a sensible value to output in main() then it works. The 'if' tests are very simple and will work.


Delete the while statement at line 11 and the associated brackets and see if you get an answer.
closed account (48T7M4Gy)
It's your lucky day! Cya

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
40
41
42
43
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int randRange (int lowest,int highest)
{
    int rand_result = rand();
        if (rand_result >= lowest && rand_result <= highest)
            return rand_result;
}

int main()
{
    int input;
    srand(time(NULL));
    int output = randRange(0, 100);
    
    cout << "I picked a number from 1 to 100, guess which one: "<<endl;
    cin >> input;
    while (1)
    {
        if (input < output)
        {
            cout << "too low"<<endl;
            cin >> input;

        }
        else if (input > output)
            {
                cout << "too high"<<endl;
                cin >> input;
            }
        else if (input == output)
            {
                cout << "right"<<endl;
                break;
            }
    }
    return 0;
}
sorry but that isnt working either..
closed account (48T7M4Gy)
It runs on the shell here. That's where I tested and debugged it.


If it's not exactly what you want then by all means feel free to change it. Either that or give it a miss altogether. :)
well it runs, but the random output provided isnt between 1 and 100. :/
closed account (48T7M4Gy)
Then you'll have to fix it I guess.

http://www.cplusplus.com/reference/cstdlib/rand/
sure, thanks
Topic archived. No new replies allowed.