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.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace 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;
}