### 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

Problem 1: 4
+ 3
----

Congratualations Austin, you are correct!

Problem 2: 7
+ 0
----

Congratualations Austin, you are correct!

Problem 3: 6
+ 1
----

Congratualations Austin, you are correct!

Problem 4: 5
+ 2
----

Congratualations Austin, you are correct!

Problem 5: 9
+ 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 five = 5;
double a, b, c;
//int correctCount = 0, incorrectCount = 0;
int main()
{
unsigned seed = time(0);
srand(seed);

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

getline(cin, name);
cout << "\nYou did not enter one of the choices for grade!";
return 1;
}//end if

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 <<"\nCongratualations " << name << ", you are correct!\n"; correctCount++;
} //end if

cout <<"\nSorry " << name <<", you are incorrect. The correct answer was " << c << "." << endl; incorrectCount++;
} //end 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include #include #include #include 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.