Makes sense to me; Where's my logic error? (Arithmetic Generator)

So I'm very new to coding in general, and my assignment is to make a program that generates a random addition problem and the user needs to guess the answer. If it's correct, they will get a congratulations message. If not, they get an "incorrect" message. The problem here is that no matter what the user types in, it says it's correct.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

/* Write a program that can be used as a math tutor for a young student. The
program should display two random numbers that are to be added. The program
should wait for the student to enter the answer. If the answer is correct, a
message of congratulations should be printed. If the answer is incorrect, a
message should be printed showing the correct answer. */

int main()
{
    int random1, random2, answer;
    srand(time(0)); // Needed for true randomization

    random1 = rand()%500+1; // Randomizes between 1 and 500
    random2 = rand()%500+1;

    cout << "What is the answer to the following problem?" << endl;
    cout << "" << endl;
    cout << "  " << random1 << endl;
    cout << "+ " << random2 << endl;
    cout << "________" << endl;
    cin >> answer;

    if (answer = random1 + random2)
    {
        cout << "Congratulations! That's correct!" << endl;
    }
    else
    {
        cout << "That's incorrect :( " << endl;
    }
    return 0;
}
= is assignment
== is comparison
Topic archived. No new replies allowed.