C program. IDE used is Xcode v12.2.
The problem I am facing:
Faulty scores output. For each round, the program should output the highest and lowest scores and the average score.
What I have tried:
void quiz(char name[])
{
// function created to enclose quiz functionality apart from instructions
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0;
int i, j, g = 0;
//char x;
struct struc test[MAX_TESTS];
srand((unsigned)time(NULL));
for (;;) {
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i(&test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i(&test[i]); //then re-generate
}
//int ig = getchar();
char x;
x = getchar();
printf("\n Are you ready? Press Enter key to continue. ");
fflush(stdin);
while(x!='\n') { }
while ( getchar() != '\n') { }
//getchar();
for (i = 1; i <= 5; i++) {
printf(
" *******************************************************************"
"**"
"***********\n");
printf(
" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i(&test[i], i);
printf(" End\n\n");
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 'Q' to quit");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread("%c", &choice);
if (choice == 'r' || choice == 'R')
{
done = true;}
else if (choice == 'S' || choice == 's') {
showS = false;
// calculate total score for current round
g = 0;
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 (rounds == 1)
{
printf(" Final score: %d/100\n", g); //display round score
printf(" ****** Player: %s ******\n", name);
}
else {
//puts("Whoops! Looks like highest/lowest have not been adjusted!");
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: %f\n", avg);
printf(" ****** Player: %s ******\n", name);
}
unsure = true;
//getchar();
}
else if (choice == 'P' || choice == 'p') {
g = 0;
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
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
}
else {
puts(" Invalid input!");
unsure = true;
}
}
if (done)
break;
}
}
Scores output after playing for 1 round:
Questions:
In the first pic, why is ‘Round 1’ shown as ‘Round 0’? And what does the ‘inf’ mean in the average score section? Please help me turn ‘inf’ into a numerical output. The same thing happens after playing for two rounds.
Any help would be greatly appreciated.