I have a program here that runs addition or subtraction randomly using two randomly generated numbers. The user then has to input the answer and if it right it says "Correct" and if it is wrong it says "Incorrect" and displays the correct answer. It seems when I run my code that it does not display the correct answer, rather an incorrect answer that it thinks is correct. For example, I ran the code and the problem I got was 950 + 921. The answer should be 1871 but it told me I was incorrect and the right answer, to the program, was 1042. I am not sure where the error is in my code but here it is if you want to take a look:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(NULL)));
cout << "Welcome to the Math Tutor!" << endl;
int N1 = rand() % 999;
int N2 = rand() % 100;
int symbol = rand() % 2;
int Result;
int Answer;
cout << setw(5) << N1 << endl;
if(symbol == 1)
{
cout << "+";
Result = N1 + N2;
}
else
{
cout << "-";
Result = N1 - N2;
}
cout << setw(3) << N2 << symbol << "\n";
cout << setw(5) << "------\n\n";
cout << "Enter your answer: ";
cin >> Answer;
if(Answer == Result)
{
cout << "You are correct!\n\n";
}
else
{
cout << "You are incorrect, the correct answer is: " << Result << "\n\n";
}
cin.ignore(1);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire