Math Tudor - Random Numbers

// Objective: Guess the correct answer of the random numbers
// Problem: I guess the correct answer but the program keeps telling me that
// I'm INCORRECT.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(int argc, const char * argv[])
{
const int MIN_VALUE = 1; // Constants
const int MAX_VALUE = 200;

int num_1, num_2, total; // Variables
char ch;
unsigned short seed = time(0); // System time

srand(seed); // Seed the random seed generator

num_1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
num_2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

total = num_1 + num_2;
cout << num_1 << endl;
cout << num_2 << endl;
cout << "_____" << endl;

cin.get(ch);

if (cin.get() == total)
{
cout << "That is the correct answer!" << endl; //<--PROBLEM
}
else
{
cout << "That is an incorrect answer. The correct answer is " << total << endl;
}
return 0;
}
1
2
cin.get(ch);
if(cin.get() == total)
should be something like
1
2
3
int input = 0;
cin >> input;
if(input == total)


You are reading in a single character and trying to compare it to a number that is probably more than 1 character and anyways you would be comparing the ascii value of any digit entered to the added value. I would also suggest you make the input/output more user friendly such as
1
2
3
4
5
6
7
8
9
10
11
cout << "What is " << num_1 << " + " << num_2? " << std::endl;
cin >> input;

if(input == total)
{
    cout << "Correct!" << endl;
}
else
{
    cout << "Incorrect." << endl;
} 
I've been laying off this problem for a month. I don't remember why I did what I did. I did it your way and it worked. Thank you.
Topic archived. No new replies allowed.