Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective Learn to modify existing code to add new functionality Learn to work with functions, loops, and files Concepts Loops File Input and Output Code

Objective

Learn to modify existing code to add new functionality

Learn to work with functions, loops, and files

Concepts

Loops

File Input and Output

Code Modification

Description

In this program you will modify the code you created for Program 1

While answering a single math question is useful, to be an effective tudor answering multiple questions is essential. In addition, being able to see some statistics on how well you have performed both in the current session and overall can help as you track your progress. For these reasons, we will be altering your original code to add this functionality. To accomplish this work you will need to enclose your existing code inside of the appropriate loop, with the correct control structure. We will also use a file saved to the working directory that will store long term statistics called Math.txt. When the user terminates your program, it should notify them of how many questions were answered correctly and how many were ask. It should also report the percentage of correctly answered questions. In addition, it will report the cumulative statistics for the program.

Input

The program should open a file stored in the same location as the program entitled Math.txt. In this file will be two lines of data the number of questions answered and the number of correct (or incorrect) answers provided. The tudor will proceed as before with the computer asking the user simple math questions, with a new input option to quit the program.

Output

Each time the tudor is played the program should output the current question as well as the result of the users input, as in Program 1. In addition, once the user quits the tudor, statistics should be displayed showing the total number of questions this session and the number the user was correct. Finally, using data in the provided input file the user should be shown how many questions they have been asked overall and the number of correct answers overall they have. This data should then be written to the math.txt file so that the numbers are accurate and can be read in the next time the user plays

Input Validation

If the user enters an option other than y or n for the question you should alert them that they entered an incorrect value and reprompt them for a correct option. You should continue to prompt for a valid entry until one is provided. If the RPS.txt file does not exist, one should be created, with 0's assigned for both all time statistics.

Sample:

C:\CSC2100\Program1> ModMath.exe Please enter your answer to the following question: 39 + 275 ----- 390 I am sorry, that is incorrect. The correct answer is 309. Would you like to play again? (y/n) : y 385 + 639 ----- 1124 I am sorry, that is incorrect. The correct answer is 1024. Would you like to play again? (y/n) : l That is an incorrect option. Please enter either a y or an n. Do you wish to play again (y/n) : y 875 + 5 ----- 880 That is correct! Would you like to play again? (y/n) : n This session you had 1 correct answer on 3 questions meaning you scored a 33.3%. Overall you have answered 12 questions correctly out of 22 asked for a score of 54.5%

Sample Output

MATH TUDOR PROGRAM 1

#include

#include

#include

#include

using namespace std;

//Function to seed the randomizer with the value of seconds in the current time

//Pre: None

//Post: Randomizer will generate different values each time run

void seedRandom()

{

srand (time(NULL));

}

//Function to generate a random number between lo and hi

//Pre: lo and hi are integers such that lo is less than hi

//Post: Generates a random number, lo inclusive, hi exclusive

int getRandomInt(int lo, int hi)

{

return rand() % (hi - lo) + lo;

}

int main()

{

//Define Constants

const int LOW = 1;

const int HIGH = 1000;

//Define Variables

int top, //Top number to be used for addition

bottom, //Bottom number to be used for addition

answer, //Top + Bottom

userGuess; //User answer

seedRandom(); //Call to guarantee different random numbers

top = getRandomInt (LOW, HIGH); //Get top number

bottom = getRandomInt (LOW, HIGH); //Get bottom number

answer = top + bottom;

//Output stuff

cout << "Welcome to the Math Tudor" << endl;

cout << setw(5) << top << endl;

cout << "+ " << setw(3) << bottom << endl;

cout << "-----" << endl;

cout << " ";

cin >> userGuess;

//Check user answer for correctness

if (userGuess == answer)

{

cout << "That is the correct answer. Congrats" << endl;

}

else

{

cout << "Sorry, but that is not the correct answer." << endl;

cout << "The correct answer is : " << answer << endl;

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the advantages and disadvantages of e-mail?

Answered: 1 week ago

Question

3. Management of information and interaction.

Answered: 1 week ago