C program. IDE used is Xcode v12.2
After typing the letter 'S' to show results, the program displays two outputs. But when entering the letters 'P' or 'R', the program displays normal output.
Code:
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure)
{
unsure = false;
puts("\n");
if (showS)
{
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
scanf("%c", &choice);
if (choice == 'r' || choice == 'R')
{
done = true;
}
else
{
///////////////////////// Changes /////////////
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++)
{
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
if (choice == 'S' || choice == 's')
{
showS = false;
if (rounds == 1)
{
printf(" Final score: %d/100\n", g); //display round score
printf(" ******Player: %s ******\n", name);
}
else
{
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score: %d/100\n", lowest);
printf(" Average score: %d/100\n", avg);
printf(" ******Player: %s ******\n", name);
}
unsure = true;
}
else if (choice == 'P' || choice == 'p')
{
/// nothing to be done here
//we will display next test
}
else
{
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
Any advice would be greatly appreciated.