Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 if the player required so.

Scores output after playing for 1 round:

Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to the main menu
S
Round 0 score: 92/100
Highest score: 92/100
Lowest score: 92/100
Average score:inf
****** Player: MAX ******


Scores output after playing for 2 rounds:
Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to the main menu
S
Round 0 score: 95/100
Highest score: 95/100
Lowest score: 92/100
Average score:inf
****** Player: MAX ******


Questions:

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. 'Round 2' is still shown as 'Round 0' and the 'Average score' did not change to a numerical output.

Any help would be greatly appreciated.

What I have tried:

C++
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 the 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;
    }
}
Posted
Updated 12-Dec-20 4:12am
v2

To add to what Richard has said, the inf problem is pretty obvious. Look at your output:
Round 0 score: 95/100
...
Average score:inf
Look at the code that generates it:
C++
printf(" Round %d score: %d/100\n", rounds, g); //display round score
...
printf(" Average score: %f\n", avg);
So we know that rounds is zero.
Look at the code to generate avg:
C++
avg = allScore / rounds;
And you immediately see the source of the inf value: division by zero.

To be honest, thirty seconds with the debugger would have shown you that!
   
Comments
maniacdancer 12-Dec-20 10:30am
   
I missed out
rounds++;
So now all is well, I apologize for my carelessness lol.
OriginalGriff 12-Dec-20 10:44am
   
No problem - we all do it!
But seriously, 30 seconds with the debugger ... it's really worth learning how to at least use the basics, it's pretty much the best friend you have!
maniacdancer 12-Dec-20 10:49am
   
Thank you! Will do so in the future!
Round 1 is shown as Round 0 because the value you are using in the print statement has not been incremented correctly (by you). The word "inf" for the average signifies that the value calculated is too large for a floating point number. So you need to check the values you are using in your calculations.

Use some known values as input and find out where the calculated values are wrong.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900