Question
Can you help me fix this c++ code? I originally had a function at the very end to calculate the percentages. However, I have to
Can you help me fix this c++ code? I originally had a function at the very end to calculate the percentages. However, I have to take it out because we can only have 9 functions totals in the program. Now, everything is working fine except my percentages at the very end.
#include
#include
#include
using namespace std;
//Function Prototypes
void getProbsPerSet(int* probsPerSet);
void getMaxNum(int* maximum);
void doOneSet(char sign, int probsPerSet, int& correct);
void doOneProblem(char problemType, int& randomNumberOne, int& randomNumberTwo, int maximum);
void generateOperands(char problemType, int& randomNumberOne, int& randomNumberTwo, int maximum);
void printHeader (char sign);
void calcCorrectAnswer (char problemType, int randomNumberOne, int randomNumberTwo, int& answer);
void checkAnswer (int correctAnswer, int answer, int& numberCorrect);
void printReport (int setOne, int setTwo, int setThree, int probsPerSet);
int main()
{
//Variable Definitions
int probsPerSet = 0;
int set1Correct = 0, set2Correct = 0, set3Correct = 0;
//Function Calls
srand(static_cast
getProbsPerSet(&probsPerSet);
doOneSet('+', probsPerSet, set1Correct);
doOneSet('-', probsPerSet, set2Correct);
doOneSet('*', probsPerSet, set3Correct);
printReport(set1Correct, set2Correct, set3Correct, probsPerSet);
}
//*************************************************************************************
// Function Definition
// This function asks the user how many problems per set they want.
// Problems per set must be between 3 and 10.
//*************************************************************************************
void getProbsPerSet(int* probsPerSet)
{
cout << "Enter problems per set (3-10): ";
cin >> *probsPerSet;
}
//*************************************************************************************
// Function Definition
// This function does a set of math problems problems.
// Math problems include adding, subtracting, and multiplying 2 random numbers.
// Different types of problems are generated using a switch statement.
// An answer is input by the user. The user is told whether or not their answer is right.
//*************************************************************************************
void doOneSet(char sign, int probsPerSet, int& score)
{
printHeader(sign);
//Get max number for each set
int maximum = 0;
getMaxNum(&maximum);
int answer = 0;
for(int mathProblem = 0; mathProblem < probsPerSet; mathProblem++)
{
int randomNumberOne = 0;
int randomNumberTwo = 0;
int correctAnswer = 0;
doOneProblem(sign, randomNumberOne, randomNumberTwo, maximum);
calcCorrectAnswer(sign, randomNumberOne, randomNumberTwo, correctAnswer);
cin >> answer;
checkAnswer(correctAnswer, answer, score);
}
}
//*************************************************************************************
// Function Definition
// This function asks the user for the maximum number in each set.
//*************************************************************************************
void getMaxNum(int* maximum)
{
cout << "What is the maximum number for this set? ";
cin >> *maximum;
cout << endl;
}
void calcCorrectAnswer(char problemType, int randomNumberOne, int randomNumberTwo, int& answer)
{
switch (problemType)
{
case '+':
answer = randomNumberOne + randomNumberTwo;
break;
case '-':
answer = randomNumberOne - randomNumberTwo;
break;
case '*':
answer = randomNumberOne * randomNumberTwo;
break;
}
}
void checkAnswer(int correctAnswer, int answer, int& numberCorrect)
{
if (answer == correctAnswer)
{
cout << "Correct" << endl;
numberCorrect++;
}
else
{
cout << "Incorrect" << endl;
}
}
void printHeader(char sign)
{
//If it's an addition problem, output a "Set #1" header.
if (sign == '+')
{
cout << "Set #1" << endl;
cout << "----------" << endl;
}
//If it's a subtraction problem, output a "Set #2" header.
else if (sign == '-')
{
cout << endl;
cout << "Set #2" << endl;
cout << "----------" << endl;
}
//If it's a multiplication problem, output a "Set #3" header.
else if (sign == '*')
{
cout << endl;
cout << "Set #3" << endl;
cout << "----------" << endl;
}
}
void doOneProblem(char problemType, int& randomNumberOne, int& randomNumberTwo, int maximum)
{
randomNumberOne = rand() % maximum;
randomNumberTwo = rand() % maximum;
generateOperands(problemType, randomNumberOne, randomNumberTwo, maximum);
}
void generateOperands(char problemType, int& randomNumberOne, int& randomNumberTwo, int maximum)
{
randomNumberOne = rand() % maximum;
randomNumberTwo = rand() % maximum;
switch(problemType)
{
case '+':
cout << randomNumberOne << "+" << randomNumberTwo << "= ";
break;
case '-':
cout << randomNumberOne << "-" << randomNumberTwo << "= ";
break;
case '*':
cout << randomNumberOne << "*" << randomNumberTwo << "= ";
break;
}
}
void printReport (int setOne, int setTwo, int setThree, int probsPerSet)
{
int setOnePercentage = (setOne / probsPerSet) * 100;
int setTwoPercentage = (setTwo / probsPerSet) * 100;
int setThreePercentage = (setThree / probsPerSet) * 100;
int totalPercentage = ((setOne + setTwo + setThree) / (3*probsPerSet)) * 100;
cout << "Set #1: You got " << setOne << " correct out of " << probsPerSet << " for " << setOnePercentage << "%" << endl;
cout << "Set #2: You got " << setTwo << " correct out of " << probsPerSet << " for " << setTwoPercentage << "%" << endl;
cout << "Set #3: You got " << setThree << " correct out of " << probsPerSet << " for " << setThreePercentage << "%" << totalPercentage << "%" << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started