Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cmath>
- #include <time.h>
- #include <string>
- using namespace std;
- //Global for file output
- const string ADD = "Addition";
- const string SUB = "Subtraction";
- const string MULTIPLY = "Multiplication";
- //function prototypes
- void menu(int &);
- void saveFile(ofstream &, int, string);
- //main function for operators
- int main()
- {
- //Variables for menu
- int selection = 0;
- const int QUIT = -1;
- //Output file
- ofstream worksheet;
- worksheet.open("worksheet.txt");
- //file failures
- if (worksheet.fail())
- {
- cout << "CANNOT OPEN WORKSHEET.TXT, EXITING.../n/n";
- system("pause");
- exit(1);
- }
- //actual output file contents
- worksheet << "Operation:"
- << setw(20)<< "Attempts:";
- //variables for numbers, attempts, input for user, operator
- const int MAX_NUM = 999;
- int num1, num2, answer, userAnswer, attempts;
- userAnswer = 0;
- //variable for operator
- string operate;
- //Seed randomizer for operations
- srand(unsigned int(time(NULL)));
- while (selection != QUIT)
- {
- //Run menu function, get selection
- menu(selection);
- //Addition
- if (selection == 1)
- {
- //If this is selected, sets string to add to
- //put addition in txt file, rolls numbers to
- //be added together
- operate = ADD;
- attempts = 1;
- num1 = (rand() % MAX_NUM) + 1;
- num2 = (rand() % MAX_NUM) + 1;
- answer = num1 + num2;
- cout << endl << endl << setw(35) << num1 << endl;
- cout << setw(30) << "+" << setw(5) << num2 << endl;
- cout << setw(35) << "-----------" << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- //If user gets the answer wrong, prompt for reenetry
- while (userAnswer != answer)
- {
- cout << endl << "Sorry, try again!!" << endl;
- attempts++;
- cout << "Current attempt: " << attempts << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- }
- //Once answer is correct, call file out function
- //to record operation and how many attempts it
- //took to get the correct answer
- cout << "Congratualtions!!!" << endl;
- cout << "Returning to menu..." << endl;
- saveFile(worksheet, attempts, operate);
- system("pause");
- system("cls");
- }//Addition
- //Subtraction
- else if (selection == 2)
- {
- //If this is selected, sets string to SUB to
- //put subtraction in txt file, rolls numbers to
- //be subtracted
- operate = SUB;
- attempts = 1;
- num1 = (rand() % MAX_NUM) + 1;
- num2 = (rand() % MAX_NUM) + 1;
- //If num2>num1, re-roll numbers to ensure
- //no negative answers, keeps it simple
- while (num2 > num1)
- {
- num1 = (rand() % MAX_NUM) + 1;
- num2 = (rand() % MAX_NUM) + 1;
- }
- //Sets answer, displays the numbers and operator
- //prompts for answer
- answer = num1 - num2;
- cout << endl << endl << setw(35) << num1 << endl;
- cout << setw(30) << "-" << setw(5) << num2 << endl;
- cout << setw(35) << "-----------" << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- //If user gets the answer wrong, prompt for reenetry
- while (userAnswer != answer)
- {
- cout << endl << "Sorry, try again!!" << endl;
- attempts++;
- cout << "Current attempt: " << attempts << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- }
- //Once answer is correct, call file out function
- //to record operation and how many attempts it
- //took to get the correct answer
- cout << "Congratualtions!!!" << endl;
- cout << "Returning to menu..." << endl;
- saveFile(worksheet, attempts, operate);
- system("pause");
- system("cls");
- }//Subtraction end
- //Multiplication
- else if (selection == 3)
- {
- //If this is selected, sets the string
- //to MULTIPLY to put Multiplication in
- //txt file, rolls number to be multiplied
- operate = MULTIPLY;
- attempts = 1;
- num1 = (rand() % MAX_NUM) + 1;
- num2 = (rand() % MAX_NUM) + 1;
- //Sets answer, displays output of numbers,
- //prompts for answer
- answer = num1 * num2;
- cout << endl << endl << setw(35) << num1 << endl;
- cout << setw(30) << "*" << setw(5) << num2 << endl;
- cout << setw(35) << "-----------" << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- //If user gets the answer wrong, prompt for reenetry
- while (userAnswer != answer)
- {
- cout << endl << "Sorry, try again!!" << endl;
- attempts++;
- cout << "Current attempt: " << attempts << endl;
- cout << "Please enter your answer: ";
- cin >> userAnswer;
- }
- //Once answer is correct, call file out function
- //to record operation and how many attempts it
- //took to get the correct answer
- cout << "Congratualtions!!!" << endl;
- cout << "Returning to menu..." << endl;
- saveFile(worksheet, attempts, operate);
- system("pause");
- system("cls");
- }//Multiplication
- }
- cout << "You can view the operators and attempts at worksheet.txt" << endl;
- system("pause");
- return 0;
- }//main
- //Menu
- void menu(int &selction)
- {
- cout << "Welcome to the math work along program!" << endl;
- cout << "This will generate 2 numbers with the selected operator and\n allow you to solve the equation" << endl;
- cout << "1. Addition\n2. Subtraction\n3. Multiplication\nOr type -1 to quit\n";
- cin >> selection;
- while ((selection > 3) && (selection < 1) && (selection != -1))
- {
- cout << "Please enter a valid choice: ";
- cin >> selection;
- }
- }//menu
- //File write function
- //Outputs
- void saveFile(ofstream &worksheet, int attempts, string operate)
- {
- worksheet << endl << operate << setw(20) << attempts;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement