Thread: Help with this program

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    1

    Help with this program

    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?

    Code:
    • #include<iostream>
    • #include<cstdlib>
    • #include<ctime>
    • #include<iomanip>
    • #include<fstream>
    • usingnamespace 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
    • constint 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();
    • return0;
    • }
    • //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
    • unsignedint number1 =0;
    • unsignedint 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 didn't enter a valid number;
    • }
    • }while(userchoice !=5)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Suggestions for readability:
    • More whitespace, please. Especially blank lines.
    • Copy and paste the raw text, not with some additional stuff like * in the beginning.

    With that in mind, for your actual question, it's as easy as to make a flowchart. Take a step away from the code, realize the flow, amend it and then turn it back into code.

    Check back if you get stuck with your flowchart. Don't try to just amend the code directly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM