Hello everyone, I am writing a program that shows the user a list of choices. 1. Addition 2. Subtraction, 3. Multiplication 4. Division. The program runs through 5 problems (randomly generated numbers) I'd like to calculate the final grade after the 5 problems have been answered. I am using a function to show if the answer is correct or wrong. This same function is calling another funtion to calculate the grade (correctAnswers/numproblems * 100) however the way I have it now this does not work.
Code:
while(choice != 5)
{
switch (choice)
{
case 1: // Addition
{
for(i = 1; i <= PROBLIM; i++)
{
opSymbol = '+';
num1 = twoDigitNumber();
num2 = twoDigitNumber();
correctAnswer = addition(num1, num2);
showOperation(num1, num2, opSymbol);
scanf("%d", &studentAnswer);
checkAnswer(studentAnswer, correctAnswer, i); //here is where I try to calculate the grade
}
break;
// this is the funtion that checks to see if the answer is right or wrong
void checkAnswer(int studentAnswer, int correctAnswer, int a)
{
double correctCounter = 0.0, problemCounter = 0.0;
if (studentAnswer == correctAnswer)
{
printf("Correct!\n\n");
correctCounter++;
}
else
{
printf("Incorrect, Answer is: %d\n\n", correctAnswer);
problemCounter++;
correctCounter++;
}
if (a ==5)
{
printf("%.2f\n\n", grade(ptr_correctCounter, ptr_problemCounter)); // here i call the grade funtion
}
return;
}
double grade(double a, double b)
{
return ((a/b) * 100); //should return a percentage
}
I know the reason it doen't work is bc each time i call the checkAnswer funtion both counters get reset back to 0.0 anyone know of a better perhaps simpler way of doing this?
Thank you for any help in advance and let me know if you need any clarification