| CODE |
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void displayHeader(); void problemType(); void calcRealAnswer(); bool goAgain(); int calculateRandomNumber(); int choice = 0; int userAnswer = 0; int realAnswer = 0; float randomNumber = 0; int main(){ srand((unsigned)time(0)); do { displayHeader(); problemType(); }while(goAgain()); system("cls"); cin.get(); cin.get(); return 0; } void displayHeader() { cout<<"Welcome to my math game program. Sit back and enjoy!!"<<endl; cout<<endl; } void problemType() { cout << "Enter the number for the problem type desired:" << endl; cout << "1. Addition" << endl; cout << "2. Subtraction" << endl; cout << "3. Multiplication" << endl; cout << "Enter choice: "; cin >> choice; if (choice==1){ cout << calculateRandomNumber(); cout << " + "; cout << calculateRandomNumber(); cout << " = "; cin >> userAnswer;} else if (choice==2){ cout << calculateRandomNumber(); cout << " - "; cout << calculateRandomNumber(); cout << " = "; cin >> userAnswer;} else if (choice==3){ cout << calculateRandomNumber(); cout << " * "; cout << calculateRandomNumber(); cout << " = "; cin >> userAnswer;} else{ cout << "Invalid answer. Please select a valid choice: "; cin >> choice; } } bool goAgain() { char answer; cout << "Would you like to play again? "; cin >> answer; system("cls"); while(answer != 'Y' && answer != 'N') { cout << "Invalid choice. Would you like to go again? "; cin >> answer; } cout << endl; if(answer == 'Y') return true; else return false; } int calculateRandomNumber(){ return (rand()%100) + 1; } |
| CODE |
if( userAnswer == ( calculateRandomNumber() + calculateRandomNumber() ) ) cout << "You RIGHT!!\n"; } else cout << "Your wrong!!\n"; |
| CODE |
switch ( choice ) { case 1: cout << variable1 << ' + ' << variable2 << ' = '; cin >> userAnswer; if( userAnswer == ( variable1 + variable2 ) ) cout << "You RIGHT!!\n"; } else cout << "Your wrong!!\n"; break; ............ } |
| CODE |
srand((unsigned)time(0)); |
| CODE |
srand(time(0)); |