0

How do I change this code so that the user can choose a math operation and determine have that type of operation(either add sub mult or divide) run as much as a user wants it to? Say, if the user wanted 5 addition questions only.

And how do I keep track of the correct answers that they get?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

// Function prototypes
int getuserchoice();
int getNumQuestions();
int getHighestNum();
void showmenu();
void showcorrectanswer(int, int);

//Variables
int previoususer;
int number1, number2;
int numquestions;
int highestNum;
char username;
int userchoice;
int studentanswer;
float correctanswer;
string newuser;


// Variables to keep track of scores
int userscore = 0;

//Constants for the menu choices
const int ADDITION_CHOICE = 1,
          SUBTRACTION_CHOICE = 2,
          MULTIPLICATION_CHOICE = 3,
          DIVISION_CHOICE = 4,
          QUIT_CHOICE = 5;

int main()
{

    cout << "This program will display a menu of math operations. You will enter your name then choose an option for the menu. The program will save your scores and keep track of it. Let's get started!\n\n" << endl;

    cout << "The previous user is: "<< previoususer << endl;
    cout << endl;
    cout << "What is your name?" << endl;
    cin >> newuser;

    cout << "Hi, " << newuser << endl;

    // Get userchoice
    userchoice = getuserchoice();

    // Get number of questions
    numquestions = getNumQuestions();

    // Get highest number
    highestNum = getHighestNum();


    //Write name to file
    ofstream filenames;
    filenames.open("nameuser.txt");
    filenames << newuser;

    filenames.close();

    cout << newuser;



    // Show menu
    showmenu();

    return 0;

}


//GET USERCHOICE FUNCTION
int getuserchoice()
{
cout << "Choose from the following options:\n\n"
<< "1. Addition\n"
<< "2. Subtraction\n"
<< "3. Multiplication\n"
<< "4. Division\n"
<< "5. Quit\n\n"
<< "Enter a number from 1 to 5 and hit Enter: \n";

    cin >> userchoice;
    return userchoice;
}

//NUMBER OF QUESTIONS FUNCTION
int getNumQuestions()
{
cout << "How many questions do you want to answer? "<< endl;
    cin >> numquestions;
    return numquestions;
}

// GET HIGHEST NUM FUNCTION
int getHighestNum()
{
    cout << "What is the highest number that you want in your questions?" << endl;
    cin >> highestNum;
    return highestNum;

}

void showmenu()
{
do
{ // Beggining of do-while loop
    unsigned int number1 =0;
    unsigned int number2 =0;
    srand(unsigned(time(0)));

    number1 = 1+rand() % highestNum;
    number2 = 1+rand() % highestNum;


    // Validates the input (must not be less than 1/greater than 5)
    if (userchoice < ADDITION_CHOICE || userchoice > QUIT_CHOICE)

    {

        cout << "Please choose a valid number from 1 to 5\n\n"
        << "1. Addition\n"
        << "2. Subtraction\n"
        << "3. Multiplication\n"
        << "4. Division\n"
        << "5. Quit\n\n"
        << "Enter your choice: ";
        cin >> userchoice;
    }

    if (userchoice == 5) {

        cout << "goodbye" << endl;
    }


    //Setting the decimal to two decimal places.
    cout << setprecision(2) << fixed << endl;

    // Respond to user's choice
    switch (userchoice)

    { // Addition
        case ADDITION_CHOICE:
            cout << "Solve the problem below: \n";
            cout << number1 << " + " << number2 << " = " << endl;
            correctanswer = number1 + number2;
            cin >> studentanswer;
            if (studentanswer == correctanswer)
                cout << "Correct! Congratulations.\n";
            else
                cout << "That is incorrect. The correct answer is " << correctanswer << endl;
            break;

            // Subtraction
        case SUBTRACTION_CHOICE:
            cout << "Solve the problem below: \n";
            cout << number1 << " - " << number2 << " = " << endl;
            correctanswer = number1 - number2;
            cin >> studentanswer;
            if (studentanswer == correctanswer)
                cout << "Correct! Congratulations.\n";
            else
                cout << "That is incorrect. The correct answer is " << correctanswer << endl;
            break;

            // Multiplication
        case MULTIPLICATION_CHOICE:
            cout << number1 << " * " << number2 << " = " << endl;
            correctanswer = number1 * number2;
            cin >> studentanswer;
            if (studentanswer == correctanswer)
                cout << "Correct! Congratulations.\n";
            else
                cout << "That is incorrect. The correct answer is " << correctanswer << endl;
            break;

            // Division
        case DIVISION_CHOICE:
            cout << "Round your answer to two decimal places." <<endl;
            cout << number1 << " / " << number2 << " = " << endl;
            correctanswer = (float)number1 / number2;      // typecast float
            cin >> studentanswer;
            if (studentanswer<correctanswer+0.05 && studentanswer>correctanswer-0.05)
                cout << "Correct! Congratulations.\n";
            else
                cout << "That is incorrect. The correct answer is " << correctanswer << endl;
            break;

            // Exit
        case QUIT_CHOICE:
            exit(0);
            break;

            // Default
        default: cout << "you dudnt enter ya";


    }





    }while(userchoice != 5);

}
2
Contributors
1
Reply
17
Views
3 Years
Discussion Span
Last Post by David W
0

... code so that the user can choose a math operation and ... have that type of operation(either add sub mult or divide) run as much as a user wants ... Say, if the user wanted 5 addition questions only.

And how do I keep track of the correct ... ( ++counterCorrect )

I would suggest you start fresh with a shell that does just the essence of what you want ...
// the shell could just include all the comments listed below ...
//and give some simple output ...
//to verify you GET that expected output!!!

Compile and debug that at FIRST!

THEN ... and ONLY THEN ...
fill in the sections of that shell
section by section ...

test and debug each section INDIVIDUALLY
(perhaps ... some of the code blocks in your example code may do, or guide you in coding to do, a job requested) ...

USE a separate function for each job ...
(add, sub, mul, div)

//TopOfLoop:

//show menu

//prompt and input choice

//prompt and input the number of questions to show and get answered

//do the job (function) requested, presenting then, the number of questions requested, and counting the number of correct answers as you go

//then report all the results

//the kind of job ...
//the number of questions
//the number correct

//prompt and ask if more ... 
//i.e. do you want to loop again from top

Edited by David W: added comments

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.