arithmetic problem, wont evaluate

So Im continuing a past assignment by making the user answer multiple questions depending on their grade level. After they answer the questions I need to provide a summary including how many they got right and wrong, and also the percent correct. The problem I am having is the percent correct part wont evaluate right. It only works when the correctCount is 5, then it will evaluate to 100%. I am wondering if I am declaring the variable in the wrong scope/type, or if i need to write a separate function to call into my main. I'll bold the problem line. Also included is an example of what the current grade 1 looks like with the summary not working correctly.

Welcome to the Math Tutor

Enter your name: Austin
Enter your grade (1, 2, or 3): 1

Please answer the following problem Austin.


Problem 1: 4
+ 3
----
Answer: 7

Congratualations Austin, you are correct!

Problem 2: 7
+ 0
----
Answer: 7

Congratualations Austin, you are correct!

Problem 3: 6
+ 1
----
Answer: 7

Congratualations Austin, you are correct!

Problem 4: 5
+ 2
----
Answer: 7

Congratualations Austin, you are correct!

Problem 5: 9
+ 0
----
Answer: 0

Sorry Austin, you are incorrect. The correct answer was 9.
4
=================================================
Summary: Austin, you got 4 correct and 1 incorrect, so you got 0% correct!

#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;

string name;
int grade;
int five = 5;
double a, b, c;
double userAnswer;
//int correctCount = 0, incorrectCount = 0;
int main()
{
unsigned seed = time(0);
srand(seed);

cout <<"Welcome to the Math Tutor\n\n";

cout <<"Enter your name: ";
getline(cin, name);
cout <<"Enter your grade (1, 2, or 3): ";
cin >> grade;
if (grade > 3 || grade < 1) {
cout << "\nYou did not enter one of the choices for grade!";
return 1;
}//end if

cout <<"\nPlease answer the following problem "<< name <<".\n"<< endl;

if(grade == 1){ int i = 0; int correctCount = 0; int incorrectCount = 0;

for ( i = 1; i < 6; i++) {
//int correctCount = 0;
//int incorrectCount = 0;
a = rand() % 10;
b = rand() % 10;
c = a + b;
cout <<"\nProblem " << i << ":";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(14);
cout <<"+ " << b << endl;
cout << right << setw(15);
cout <<"----"<< endl;
cout << "Answer: ";
cin >> userAnswer;

if(c == userAnswer){
cout <<"\nCongratualations " << name << ", you are correct!\n"; correctCount++;
} //end if

else if(c != userAnswer){
cout <<"\nSorry " << name <<", you are incorrect. The correct answer was " << c << "." << endl; incorrectCount++;
} //end if
} //end grade 1 if

if ( i = 6 ) { cout << correctCount; // to check correctCount is coming out right
double percentCorrect = (( correctCount / five ) * 100);
cout << "\n=================================================\n";
cout << "Summary: " << name << ", you got " << correctCount << " correct and " << incorrectCount << " incorrect, so you got "
<< percentCorrect << "% correct!\n";
}//end summary if
//end for loop
}//end main
I commented out the other variable declarations I tried that didnt work. Thanks for any help, I have to repeat this same thing for a grade 2 and grade 3.
*****edited for closing brackets
Last edited on
You are missing a couple of the close brackets. These things }

This line here didn't have closing bracket.
 
if(grade == 1){ int i = 0; int correctCount = 0; int incorrectCount = 0;


Also your Main function didn't have a closing bracket. Beyond that IDK.
that is only a third of my code. i already have the grade 2 and 3 parts written but it is really long so I only put this section in. once i figure out this error I can easily finish this program. This part compiles fine and runs everything, except for calculating the percent part.

I closed the brackets mentioned by Garion
Last edited on
Global constants are OK, avoid global variables like you have.

Watch your opening and closing brackets.

five is a int double percentCorrect = (( correctCount / five ) * 100); switch it to a double.


Fixed some of the issues you have. Hope this points you in the direction you need.

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
#include <string>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <iostream>

using namespace std;

int main(){

	string name;
	int grade;
	double a, b, c, userAnswer, percentCorrect, five = 5;
	srand(unsigned(time(NULL)));

	cout << "Welcome to the Math Tutor\n\n";

	cout << "Enter your name: ";
	getline(cin, name);
	
	cout << "Enter your grade (1, 2, or 3): ";
	cin >> grade;
	
	if (grade > 3 || grade < 1) {
		cout << "\nYou did not enter one of the choices for grade!";
		return 1;
	}//end if

	cout << "\nPlease answer the following problem " << name << ".\n" << endl;

	if (grade == 1){
		int i = 0; int correctCount = 0; int incorrectCount = 0;

		for (i = 1; i < 6; i++) {
			a = rand() % 10;
			b = rand() % 10;
			c = a + b;
			cout << "\nProblem " << i << ":";
			cout << right << setw(5);
			cout << a << endl;
			cout << right << setw(14);
			cout << "+ " << b << endl;
			cout << right << setw(15);
			cout << "----" << endl;
			cout << "Answer: ";
			cin >> userAnswer;

			if (c == userAnswer){
				cout << "\nCongratualations " << name << ", you are correct!\n"; correctCount++;
			} //end if

			else if (c != userAnswer){
				cout << "\nSorry " << name << ", you are incorrect. The correct answer was " << c << "." << endl; incorrectCount++;
			} //end if
		} //end grade 1 if

		if (i = 6) {
			cout << correctCount; // to check correctCount is coming out right
			percentCorrect = ((correctCount / five) * 100);
			cout << "\n=================================================\n";
			cout << "Summary: " << name << ", you got " << correctCount << " correct and " << incorrectCount << " incorrect, so you got "
				<< percentCorrect << "% correct!\n";
		}//end summary if
	}//end for loop
	return 0;
}//end main 
Last edited on
Topic archived. No new replies allowed.