Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Ch5Ex8
- //
- // Created by Bertoski on 10/01/12.
- // Copyright (c) 2012 Bertoski. All rights reserved.
- // This program is used as a math tutor v.2
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- int choice;//To hold menu choices
- float x = 10 + rand() % 1000;//Store x value
- float y = 1 + rand() % 1000;//Store y value
- float sum;// Store sum
- float difference;//Store difference
- float product;//Store product
- float quotient;//Store quotient
- float input; // Store input
- // Setting srand time
- srand(time(0));
- //Constants for menu choices
- const int ADDITION_CHOICE = 1,
- SUBTRACTION_CHOICE = 2,
- MULTIPLICATION_CHOICE = 3,
- DIVISION_CHOICE = 4,
- QUIT_CHOICE = 5;
- //Set the numeric output formatting.
- cout << fixed << showpoint << setprecision(2);
- do
- {
- //Display of Menu and Choices
- cout <<" \nMath tutoring problem generator\n\n"
- << "1. Addition \n"
- << "2. Subtraction \n"
- << "3. Multiplication \n"
- << "4. Division \n"
- << "5. Quit \n"
- << "Enter your choice (1-5): ";
- cin >> choice;
- //Responce to user selection.
- while (choice < ADDITION_CHOICE || choice > QUIT_CHOICE)
- {
- cout <<"\nThe valid choices are 1 through 5. Run the \n"
- <<"program again and select one of the choices.\n";
- cin >> choice;
- }
- //Process the user's choice.
- if (choice != QUIT_CHOICE)
- //Respond to the user's menu selection.
- switch (choice)
- {
- case ADDITION_CHOICE:
- cout <<" " << x << endl; //Setting up random numbers
- cout <<" + " << y << endl; //Setting up random numbers
- cout << "_____________ \n";
- sum = (x + y);
- cin >> input;
- if(input!= sum)
- {
- cout <<"\nSorry the correct answer is : "<< sum << endl;
- }
- else
- {
- //Correct display message
- cout <<"\nCongratulations!!! \n";
- cout <<"You have answered the problem correctly! \n";
- }
- break;
- case SUBTRACTION_CHOICE:
- cout <<" " << x << endl; //Setting up random numbers
- cout <<" - " << y << endl; //Setting up random numbers
- cout << "_____________ \n";
- difference = (x - y);
- cin >> input;
- if(input!= difference)
- {
- cout <<"Sorry the correct answer is : "<< difference << endl;
- }
- else
- {
- //Correct display message
- cout <<"\nCongratulations!!! \n";
- cout <<"You have answered the problem correctly! \n";
- }
- break;
- case MULTIPLICATION_CHOICE:
- cout << x; //Setting up random numbers
- cout <<" * " << y <<" = ? "<< endl; //Setting up random numbers
- product = (x * y);
- cin >> input;
- if(input!= product)
- {
- cout <<"Sorry the correct answer is : "<< product << endl;
- }
- else
- {
- //Correct display message
- cout <<"\nCongratulations!!! \n";
- cout <<"You have answered the problem correctly! \n";
- }
- break;
- case DIVISION_CHOICE:
- cout << x; //Setting up random numbers
- cout <<" / "<< y <<" = ? "<< endl; //Setting up random numbers
- quotient = (x / y);
- cin >> input;
- if(input!= quotient)
- {
- cout <<"Sorry the correct answer is : "<< quotient << endl;
- }
- else
- {
- //Correct display message
- cout <<"\nCongratulations!!! \n";
- cout <<"You have answered the problem correctly! \n";
- }
- break;
- case QUIT_CHOICE:
- cout <<"Program ending. \n";
- }
- } while (choice !=QUIT_CHOICE);
- return 0;
- }
Add Comment
Please, Sign In to add comment