Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program name: Lab10
- *Author: Michael G!
- *Course Cosc 1435- Introduction to Problem Solving I
- *Date written: November 12, 2008
- *Purpose:The program will help a student learn addition subtraction
- *multiplication and division by giving a random problem and checking
- *if the users answer is right
- *Input:The user will imput if they want to use add subtract multiply
- *or divide. The user will also enter what they got for the problem
- *Output: After the two random numbers are added, the program will
- *display the results and say if they got the problem right or not.
- *The program will aos display error messages if invalid data is entered
- ***********************************************************************/
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- char getMenuChoice () ;
- void ProcessChoice (char choice);
- void showResults (bool answer) ;
- int doAddition (int num1, int num2) ;
- int doSubtraction (int num1, int num2) ;
- int doMultiply (int num1, int num2) ;
- float doDivision (int num1, int num2) ;
- bool getIntAnswer (int answer) ;
- bool getFloatAnswer (float answer) ;
- int main()
- {
- char choice;
- choice = getMenuChoice();
- ProcessChoice(choice);
- return 0 ;
- }
- char getMenuChoice()
- {
- char choice;
- cout << "Choose a number\n";
- cout << "1. add\n"
- << "2. subtract\n"
- << "3. multiply\n"
- << "4. divide\n"
- << "5. quit" ;
- cin >> choice;
- while (choice < '1' || choice >'5')
- {
- cout << "Re-enter choice: ";
- cin >> choice;
- }
- return choice;
- }
- void ProcessChoice (char choice)
- {
- int num1,
- num2,
- intAns;
- float floatAns;
- bool right;
- srand((unsigned)time(NULL));
- num1 = rand() % 150 + 1;
- num2 = rand() % 150 + 1;
- switch (choice)
- {
- case '1': intAns = doAddition(num1, num2);
- right = getIntAnswer(intAns);
- showResults(right);
- break;
- case '2': intAns = doSubtraction(num1, num2) ;
- right = getIntAnswer (intAns) ;
- showResults(right);
- break;
- case '3': intAns = doMultiply(num1, num2) ;
- right = getIntAnswer (intAns);
- break;
- case '4': floatAns = doDivision(num1, num2) ;
- floatAns = getFloatAnswer (floatAns);
- break;
- case '5': "Thank you for your time" ;
- default: ;
- }
- }
- void showResults (bool right)
- {
- if (right==true)
- {
- cout << "Way to go \n" ;
- }
- else
- {
- cout << "Try again \n";
- }
- }
- int doAddition (int num1, int num2)
- { int add;
- if (num1 >= num2)
- {
- add = (num1 + num2);
- }
- else if (num2 >= num1)
- {
- add = (num2 + num1);
- }
- cout << setw(5) << num1 << '\n'
- << setw(2) << "+"
- << setw(3) << num2 << '\n'
- << setw(5) << "-----" << '\n';
- return add ;
- }
- int doSubtraction (int num1, int num2)
- { int subtract;
- if (num1 >= num2)
- {
- subtract = (num1 - num2);
- cout << setw(5) << num1 << '\n'
- << setw(2) << "-"
- << setw(3) << num2 << '\n'
- << setw(5) << "-----" << '\n';
- }
- else if (num2 >= num1)
- {
- subtract = (num2 - num1);
- cout << setw(5) << num2 << '\n'
- << setw(2) << "-"
- << setw(3) << num1 << '\n'
- << setw(5) << "-----" << '\n';
- }
- return subtract ;
- }
- int doMultiply (int num1, int num2)
- { int multiply;
- if (num1 >= num2)
- {
- multiply = (num1 * num2);
- }
- else if (num2 >= num1)
- {
- multiply = (num2 * num1);
- }
- cout << setw(5) << num1 << '\n'
- << setw(2) << "*"
- << setw(3) << num2 << '\n'
- << setw(5) << "-----" << '\n';
- return multiply ;
- }
- float doDivision (int num1, int num2)
- { float division;
- if (num1 >= num2)
- {
- division = (num1 / static_cast<float>(num2));
- cout << num1 << "/" << num2 ; '\n' ;
- }
- else if (num2 >= num1)
- {
- division = (num2 / static_cast<float>(num1));
- cout << num2 << "/" << num1 ; '\n' ;
- }
- return division ;
- }
- bool getIntAnswer (int answer)
- {
- int uAnswer;
- cout << "Enter your answer \n";
- cin >> uAnswer;
- if(answer == uAnswer)
- return true;
- else
- return false;
- }
- bool getFloatAnswer (float answer)
- {
- float uAnswer;
- cout << "Enter your answer \n";
- cin >> uAnswer;
- if(answer == uAnswer)
- return true;
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement