Function decomp

Hey guys so I wrote my code for an assignment and it compiles, for the most part everything works except it will not calculate percentages correctly

The output is supposed to look like this:
Enter problems per set: 3


Set #3
----------
What is the maximum number for this set? 20
0 * 18 = 0
correct
15 * 4 = 398
incorrect
8 * 17 = 873
incorrect

Set#1: You got 2 correct out of 3 for 67%
Set#2: You got 2 correct out of 3 for 67%
Set#3: You got 1 correct out of 3 for 33%
Overall you got 5 correct out of 9 for 56%

However mine will not calculate the percentage correctly and instead outputs this:
2 + 6 = 8
correct
10 + 9 = 19
correct

Set #2
----------
What is the maximum number for this set? 2
2 - 2 = 12
incorrect
2 - 2 = 0
correct

Set #3
----------
What is the maximum number for this set? 4
3 * 2 = 6
correct
2 * 3 = 1
incorrect


Set#1: You got 2 out of 2 for 100%
Set#2: You got 1 out of 2 for 0%
Set#3: You got 1 out of 2 for 0%
Overall you got 4 correct out of 6 for 0%

Process returned 0 (0x0) execution time : 23.097 s
Press any key to continue.

Not sure how I can fix this please leave any suggestions thanks in advance



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  //********************************
// This program will ask the user to answer a series of
//arithmetic questions and will report on how the user performs.
#include <iostream>
#include <ctime> // Will be used by the srand function for time
#include <cstdlib> // will allow for random numbers to be generated.
#include <iomanip>
using namespace std;


void getProbsPerSet(int& numProbs);
// The following functions will have inputs
void doOneSet( char problemType,int numProbs,int& correctCount);// Prototype
void getMaxNum(int &maxNum); // Will need inputs
void printHeader(char problemType);
void doOneProblem(char problemType, int& MaxNum, int & correctCount);
void generateOperands(int& firstNum, int& secondNum, int maxNum);
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer);
void checkAnswer (int correctAnsw,int answer, int&correctCount);
void printReport(int probsPerSet, int set1Correct, int set2Correct, int set3Correct);


int main()
{
int probsPerSet;
int set1Correct,set2Correct, set3Correct;
srand(time(0));
getProbsPerSet(probsPerSet);
doOneSet('+', probsPerSet, set1Correct);
doOneSet('-', probsPerSet, set2Correct);
doOneSet('*', probsPerSet, set3Correct);
printReport(probsPerSet,set1Correct, set2Correct, set3Correct);   //a report is generated for the user
                                                                       //telling them how they did
}




void getProbsPerSet(int& numProbs)
//Pre: numProbs is assigned
//Post: numProbs is received from the user and passed to the calling function
{
   cout << "Enter problems per set: ";
   cin >> numProbs;
}



void doOneSet( char problemType,int numProbs,int& correctCount)
//Pre: problemType,numProbs and correctCount are assigned
//Post: output one set of problems
{

   int maxNum;
   correctCount=0;
   printHeader(problemType);
   getMaxNum(maxNum);

   for (int i = 1; i <= numProbs; i++)
   {
       doOneProblem(problemType, maxNum, correctCount);
   }

}



//******************************************************************
// Function void printerHead definition 
//prints out the header at the top of each set denoting the number
//******************************************************************
void printHeader(char problemType)
//Pre: problemType is assigned
//Post: the header is then printed
{
   int setNumber;

   if (problemType == '+')
       setNumber = 1;
   if (problemType == '-')
       setNumber = 2;
   if (problemType == '*')
       setNumber = 3;

   cout << endl << "Set #" << setNumber << endl << "----------" << endl;
}

//*************************************************************************************************
// Function void getMaxNum
// This function allows the user to enter the highest number that may b used in one of the problems
//*************************************************************************************************
void getMaxNum(int &maxNum)
{
   cout << "What is the maximum number for this set? ";
   cin >> maxNum;
}
//************************************************************************
// Function doOneProblem definition 
//creates a single problem with two random numbers and the desired operand,
//collects the user's answer, checks it and passes the number that they
//got correct up to the main function
//************************************************************************
void doOneProblem(char problemType, int& MaxNum, int & correctCount) // delivers the amount of correct problems answer to the main function
{
   int firstNum,secondNum;
   int answer;
   int correctAnswer;
   generateOperands(firstNum, secondNum, MaxNum);
   cout << firstNum << " " << problemType << " " << secondNum << " = ";
   cin >> answer;
   calcCorrectAnsw( problemType,firstNum, secondNum, correctAnswer);
   checkAnswer(answer, correctAnswer, correctCount);
}

//prints out the problem for the user to solve
void generateOperands(int& firstNum, int& secondNum, int maxNum)
{
   firstNum= rand() % maxNum + 1;
   secondNum = rand() % maxNum + 1;
}

//*************************************************************
//Function void calcCorrectAnsw definition
//calculates what the correct answer for the problem should be
//*************************************************************
void calcCorrectAnsw(char problemType, int firstNumber, int secondNum, int& answer)

{
   if (problemType == '+')
       answer = firstNumber + secondNum;
   if (problemType == '-')
       answer = firstNumber - secondNum;
   if (problemType == '*')
       answer = firstNumber * secondNum;
}
//****************************************************************************
//Function void checkAnswer definition
// :checks the user's answer 
// :prints out feedback and increases the count of correct answers for the
// report generated for the user.
//*****************************************************************************
void checkAnswer (int correctAnsw, int answer, int &correctCount) // This function Prototype checks the user's answers
//Pre: validates user answer
//Post: outputs whether or not the user entered the correct answer
{
   if (answer == correctAnsw) // switch statement used to determine the output
                             // " correct" or "incorrect" when he user inputs their answer.
   {
       cout << "correct" << endl;
       correctCount++;
   }
   else
       cout << "incorrect" << endl;
}
//***************************************************************************************************************************************************************
//Pre: Determines how many answers the user input correctly prints out a report telling the user how many problems they got correct out of the total for each set
// Post:and the percentage correct that that equals, and also a total and percentage for all the problems
//****************************************************************************************************************************************************************
void printReport(int probsPerSet, int set1Correct, int m set2Correct, int set3Correct) // Prints a report containing percentages.
//Pre: Analyzes user answers
//Post: outputs a percentage per set and an overall
{
   int totalCorrect = set1Correct + set2Correct + set3Correct;
   int total = probsPerSet * 3.0; // Assumes three sets of problems are done 
                                 // multiplies the number problems chosen by the user by *3

   cout.precision(0);
   cout.setf(ios::fixed);

   cout << endl << endl;
   cout << "Set#1: You got " << set1Correct << " out of " << probsPerSet << " for "
       << ((set1Correct / probsPerSet) * 100 )<< "%" << endl;

   cout << "Set#2: You got " << set2Correct << " out of " << probsPerSet << " for "
       << ((set2Correct / probsPerSet) * 100 )<< "%" << endl;

   cout << "Set#3: You got " << set3Correct << " out of " << probsPerSet << " for "
       << ((set3Correct / probsPerSet) * 100 )<< "%" << endl;

   cout << "Overall you got " << totalCorrect << " correct out of " << total << " for "
       << ((totalCorrect / total) * 100 )<< "%" << endl;
}


Hello cristina999,

I see a couple of problems with:

void printReport(int probsPerSet, int set1Correct, int m set2Correct, int set3Correct) // Prints a report containing percentages.

Either you typed a "m" instead of a space or you are missing a comma and type for "set2Correct".

Then for lines 172, 175, 178 and 181 you are doing integer division when it should be double division. For example set 2 should be 1 / 2 giving you the answer of 0.5 * 100, but what you actually get is 0 * 100 which prints a 0. You could fix that with:

((static_cast<double>(set1Correct)/ probsPerSet) * 100 )

I believe that should work I will test it a bit later,

You may find this useful:
http://www.cplusplus.com/forum/beginner/212677/
as it is the same problem.

Hope that helps,

Andy
Hey mate that did it! Thank you :)
The output now reads:
Enter problems per set: 3

Set #1
----------
What is the maximum number for this set? 100
92 + 68 = 160
correct
17 + 43 = 60
correct
24 + 26 = 8
incorrect

Set #2
----------
What is the maximum number for this set? 90
72 - 70 = 2
correct
74 - 16 = -45
incorrect
81 - 19 = 62
correct

Set #3
----------
What is the maximum number for this set? 20
6 * 20 = 120
correct
3 * 12 = 45
incorrect
14 * 4 = 67
incorrect

Set #1: You got 2 correct out of 3 for 67%.
Set #2: You got 2 correct out of 3 for 66.6667%.
Set #3: You got 1 correct out of 3 for 33%.
Overall you got 5 out of 9 for 56%.

Process returned 0 (0x0) execution time : 238.595 s
Press any key to continue.
































































































Hello cristina999,

Glad it worked for you although I do not know why your set 2 has a decimal portion to the answer. It did not do that fr me.

Andy
Last edited on
Topic archived. No new replies allowed.