//Saliha Babar CS1A Chapter 4, Page 221, #9
//
/************************************************************************
*
* VALIDATES USER INPUT FOR A MATH QUESTION
* ______________________________________________________________________
* This program calculates the sum of two random numbers and then
* validates user input for that question.
*
* Computation is based on the formula;
* sum = first random number + second random number
*________________________________________________________________________
* INPUT
* firstNum : first random number generated
* secondNum : second random number generated
* userAnswer : number entered by user for the question
*
*
* OUTPUT
* sum : total of first number and second number
* This program validates user input
* *********************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
int firstNum; // OUTPUT - first random number generated below 1000
int secondNum; // OUTPUT - second random number generated below 1000
unsigned seed = time(0); // INPUT - seed value to generate random numbers
int userAnswer; // INPUT - user answer for the question
int sum; // OUTPUT - sum of two random numbers
// Seed the random number generator
srand (seed) ;
// Generate two random numbers below 1000
firstNum = 1 + rand() % 1000;
secondNum = 1 + rand() % 1000;
// Calculate the total of two random numbers
sum = firstNum + secondNum;
// Greet the user :)
cout << "Hello, I'm your math tutor. Lets do a math question\n";
cout << "Press Enter to start !\n";
cin.get();
// Display the question to the user
cout << setw(6) << firstNum << endl;
cout << "+" << setw(5) << secondNum << endl;
cout << "______" << endl;
// Let user to enter answer
cout << "Think about the answer\n";
cout << "Enter your answer below.\n";
cin >> userAnswer ;
// Input validation
if (userAnswer == sum)
{
cout << "Congrats, your answer was correct!\n";
}
else
{
// Display the answer
cout << "Your answer was incorrect, Here's the correct answer\n";
cout << setw(6) << firstNum << endl;
cout << "+" << setw(5) << secondNum << endl;
cout << "______" << endl;
cout << setw(6) << sum << endl;
}
return 0;
}