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!
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
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.
#include <string>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <iostream>
usingnamespace 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
elseif (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