Assigning Bool value Question

I am trying to allow the user to guess a random number, then assign the bool value if its true or false. I do not understand why my bool value always returns 1, weather it's right or not. here is my 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
#include <iostream>
using namespace std;

int main () {
    int answer;
    int num = rand();
    cout << num << endl;
    cout << "Enter guess:" << endl;
    cin >> answer;
    
    
    bool guess;
    if (answer = num){
       guess = 1;           
    }
    else {
       guess = 0;     
    }
    
    cout << guess;
    
    system("pause");
    return 0;
}



if (answer = num){

You're assigning num to answer here. You're not comparing them. This line is the same as:

1
2
answer = num;
if(answer != 0){


You probably meant to do this:

if(answer == num){ // note == is comparison, = is assignment
Topic archived. No new replies allowed.