Number Generator - Passing generated number into a variable

"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."

My only problem is that when I try to pass the number that my generator generates to a variable, it messes everything up.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int randRange(int low, int high)
{
    return rand() % ( high - low + 1) + low;
}

int main()
{
    int userGuess;
    int actualNumber;
    srand( time( NULL ) );
    //Let's test the function to see if it works:
    for(int i = 0; i < 5; ++i)
    {
        cout << randRange(1,100) << endl;
    }
    //Well, that worked. Let's try passing the number generated into int actualNumber:
    
    
    randRange(1,100) >> actualNumber;
    cout << actualNumber << endl;
    
    //Uhhh. 
    
}



Anyone know how I can pass whatever the generator generates into a variable? Thanks


Edited and fixed some minor bugs
Last edited on
You seem to be confused. You're treating the >> operator like you would with streams from the standard library. However, what you're actually using is the bitwise right-shift operator, not the extraction operator. Surely you know how to assign a value to something?
Last edited on
Change
randRange(1,100) >> actualNumber;
to
actualNumber = randRange(1,100);
@xismn


Well normally, yes.

If I wanted to assign a value to something I would just

int value = 25;
int answer = 0;
answer = value;


Now answer = 25.

But I can't do that with a function.

randRange(1,100) = actualNumber doesn't work.
As malemale777 pointed out, you've got it switched around. Read up on rvalues and lvalues.
@malemale777


no way I was doing it backwards.... Thanks so much.

I was doing:

randRange(1,100) = actualNumber;

instead of vice versa.
Topic archived. No new replies allowed.