I am making a simple program to calculate the required exam mark based on your current mark, the % total worth of the exam on the course, and your desired mark. I was having no problems with it until I wanted a simple while loop to redo the proses if the user wanted to do so. The problem that arose was that when I have the scanf to get a char response from the user, it terminates the while loop and then the program. When I comment out the scanf line and leave the code as is (an infinite while loop) it works properly looping forever but with it, it will terminate with no errors. Any thoughts?
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int main()
{
int continueProgram = TRUE;
float currentMark = 0.0, examPercent = 0.0, desiredMark = 0.0, requiredMark = 0.0;
char response = 'y';
while (continueProgram == TRUE)
{
printf("What is your current mark in your class: ");
scanf("%f", ¤tMark);
printf("How much is you exam worth: ");
scanf("%f", &examPercent);
examPercent /= 100.0;
printf("What is your desiered mark: ");
scanf("%f", &desiredMark);
// Simple math to get mark
requiredMark = (desiredMark - currentMark * examPercent) / examPercent;
if (requiredMark > 100.0)
{
printf("\nI'm sorry but you need above 100%% to get to your required mark\nHere is a list of all the marks you can get:\n\n");
for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
{
desiredMark = currentMark * examPercent + requiredMark * examPercent;
printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
}
}
else
{
printf("\nThe mark that you need to get on the exam is: %.2f\nHere is a list of all the marks you can get:\n\n", requiredMark);
for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
{
desiredMark = currentMark * examPercent + requiredMark * examPercent;
printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
}
}
printf("\nDid you want to continue with a new grade (y or n): ");
// THIS IS WHERE I AM HAVING THE ISSUE
scanf("%c", &response );
if (response == 'y')
{
printf("\n\nResetting terminal...\n\n");
currentMark = 0.0;
examPercent = 0.0;
desiredMark = 0.0;
requiredMark = 0.0;
}
else
continueProgram = FALSE;
}
}