Click here to Skip to main content
14,775,228 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C program.

The problem I'm facing:

I need to press the 'Enter' key twice after typing the letter 'P' if I want to output the next sentence. I'm pretty sure that previously I just needed to press the 'Enter' key once in order to do so. What's surprising is that typing the letters 'S' or 'R' will only require pressing the 'Enter' key once to get the desired results. What could have possibly gone wrong?

What I have tried:

        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(" ");
            myread("%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;
    }
}
Posted
Updated 13-Dec-20 18:27pm
v3
Comments
Rick York 13-Dec-20 22:33pm
   
We can only guess because you omitted the code for the function that reads input.
Dave Kreskowiak 14-Dec-20 1:58am
   
You left out the code that is actually doing the reading, some function you called "myread".

Without being able to see that, nobody can help you.

1 solution

Quote:
What could have possibly gone wrong?

Nobody can help you because of missing code. The only hope is using the debugger to see what is going on under the hood.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

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