fork download
  1. //Saliha Babar CS1A Chapter 4, Page 221, #9
  2. //
  3. /************************************************************************
  4.  *
  5.  * VALIDATES USER INPUT FOR MATH QUESTION
  6.  * ______________________________________________________________________
  7.  * This program calculates the sum of two random numbers and then
  8.  * validates user input for that question.
  9.  *
  10.  * Computation is based on the formula;
  11.  * sum = first random number + second random number
  12.  *________________________________________________________________________
  13.  * INPUT
  14.  * firstNum : first random number generated
  15.  * secondNum : second random number generated
  16.  * userAnswer : number entered by user for the question
  17.  *
  18.  *
  19.  * OUTPUT
  20.  * sum : total of first number and second number
  21.  * This program validates user input
  22.  * *********************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <ctime>
  27. #include <cstdlib>
  28.  
  29. using namespace std;
  30.  
  31. int main() {
  32. int firstNum; // OUTPUT - first random number generated below 1000
  33. int secondNum; // OUTPUT - second random number generated below 1000
  34. unsigned seed = time(0); // INPUT - seed value to generate random numbers
  35. int userAnswer; // INPUT - user answer for the question
  36. int sum; // OUTPUT - sum of two random numbers
  37.  
  38.  
  39. // Seed the random number generator
  40. srand (seed) ;
  41.  
  42. // Generate two random numbers below 1000
  43. firstNum = 1 + rand() % 1000;
  44. secondNum = 1 + rand() % 1000;
  45.  
  46. // Calculate the total of two random numbers
  47. sum = firstNum + secondNum;
  48.  
  49. // Greet the user :)
  50. cout << "Hello, I'm your math tutor. Lets do a math question\n";
  51. cout << "Press Enter to start !\n";
  52. cin.get();
  53.  
  54. // Display the question to the user
  55. cout << setw(6) << firstNum << endl;
  56. cout << "+" << setw(5) << secondNum << endl;
  57. cout << "______" << endl;
  58.  
  59. // Let user to think about the answer
  60. cout << "Think about the answer\n";
  61. cout << "Enter your answer below.\n";
  62. cin >> userAnswer ;
  63.  
  64. // Input validation
  65. if (userAnswer == sum)
  66. {
  67. cout << "Congrats, your answer was correct!\n";
  68. }
  69. else
  70. {
  71. // Display the answer
  72. cout << "Your answer was incorrect, Here's the correct answer\n";
  73. cout << setw(6) << firstNum << endl;
  74. cout << "+" << setw(5) << secondNum << endl;
  75. cout << "______" << endl;
  76.  
  77. // Display value of the sum
  78. cout << setw(6) <<sum << endl;
  79. }
  80.  
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 5280KB
stdin
120
stdout
Hello, I'm your math tutor. Lets do a math question
Press Enter to start !
   635
+  995
______
Think about the answer
Enter your answer below.
Your answer was incorrect, Here's the correct answer
   635
+  995
______
  1630