0

I am an inexperienced programmer making a program in C which uses switch statements that respond when the user inputs specific letters such as 'A', 'B' or 'C'. I also have a default case that outputs "That's not an option, try again." It does work fine, but it prints this message for every individual character inputted. So if the user inputs "No thanks" it will say the message 8 times!

Here are some excerpts from my code which I think are important:

scanf(" %c", &PI1);

default:
printf("That's not an option. Try again.\n");
break;

It tried turning PI1 into a string limited to 1 letter (I hoped it would help) but that made the switch statement stop working because the value was not an integer.

Edit: More code

while (Scene1 == 0) {
scanf(" %c", &PI1);
switch(PI1) {
    case 'A':
        if (Scene1War == 1) {
            printf("The Goblin is cowed, and agrees to let you through if you spare his life.");
            Scene1++;
        } else {
            printf("The Goblin laughs and states that if he wanted anything he'd take it from your corpse.\n");
        }
        break;
    case 'B':
        printf("You make a terrified expression and stare intently into the doorway behind the Goblin. \nHe is confused and turns around for a second.\n");
        Scene1Dis++;
        break;
    case 'C':
        if (Scene1Dis == 1) {
            printf("You catch the Goblin by surprise and manage to gain the upper hand. He does manage to get away alive, however.");
            Scene1++;
        } else {
            printf("You and the Goblin fight viciously. In the end he is killed and you have lost 1 HP.");
            GobLife++;
            HP--;
            Scene1++;
        }
        break;
    case 'D':
        if (Class == 2) {
            printf("You stand menacingly and tell the Goblin you mean business.\n");
            Scene1War++;
        } else if (Class == 3) {
            if (Scene1Dis == 1) {
                printf("You successfully sneak by the Goblin.");
                Scene1++;
            } else {
                printf("The Goblin notices you trying to sneak past, and stops you.\n");
            }
        } else {
            printf("That's not an option. Try again.\n");
        }
        break;
    default:
        printf("That's not an option. Try again.\n");
        break;
}}

Please don't make fun of my silly Goblin game thing. It's just a project to practice.

  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please learn how to create a [mre], and remember to make it *reproducible* (but also minimal, and no your shown code is not good enough, it's ***to*** minimal) – Some programmer dude Aug 16 '22 at 12:22
  • Showing 4 lines of code isn't very helpful, maybe you should show your whole program – programmer Aug 16 '22 at 12:23
  • Please post the full code, including the problematic `switch` statement. Just those 4 lines are not enough to be able to help you. – lee-m Aug 16 '22 at 12:24
  • Sorry, I just don't know which parts are relevant – BookwormBoy Aug 16 '22 at 12:25
  • 1
    Show us at least the `switch` statement. If there are many cases (say more than four) then only show the first two and last two. Plus the default case. If there's lots of code in each case, shorten it as well to just one or two lines. And most importantly, try to create a simple `main` function using only the input and the `switch`, and build and test. If it works for you then, then add more code bit by bit, building and testing between each bit, until the code doesn't work. Then you can narrow down the problem to the last little bit you added. – Some programmer dude Aug 16 '22 at 12:26
  • I added the rest of the switch statement. – BookwormBoy Aug 16 '22 at 12:29
  • 2
    Your Goblin Game is really cool! Honestly! – sidcoder Aug 16 '22 at 12:29
  • Thanks. I was a bit worried it was not professional enough for this site. – BookwormBoy Aug 16 '22 at 12:30
  • I also recommend you learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. More specifically how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code statement by statement while monitoring variables and their values, to see what really happens. – Some programmer dude Aug 16 '22 at 12:31
  • 1
    code always runs in a context. So code should be provided inside a function. All used symbols should have proper definition (example what if `PI1` is `int` type not `char`?) Also provide an example of input and expected output compared to actual output. – Marek R Aug 16 '22 at 12:32
  • 1
    Marek R: If the user inputs a letter that is not declared within the switch statement (Like 'E'), then I would hope it would say "That's not an option, try again." However, if the user inputs more than one character, it prints the same output as many times as there are characters. Expected output is "That's not an option, try again." once. Actual output is "That's not an option, try again." many times. Also, the PI1 is defined just above the loop as a char. – BookwormBoy Aug 16 '22 at 12:34
  • Here is an example how you can demonstrate issue: https://godbolt.org/z/86c3e6E3T – Marek R Aug 16 '22 at 12:37
  • I recommend to [not use scanf](https://stackoverflow.com/a/17932267/2485966). – Ruud Helderman Aug 16 '22 at 12:40
  • Marek R: code always runs in a context. So code should be provided inside a function. I tried putting the code into functions but now none of them work and the variables are all confused – BookwormBoy Aug 16 '22 at 12:57
  • if you want to exit after printing the error, then exit – stark Aug 16 '22 at 12:57
  • "Exit after printing the error"? Exit the loop? But then the user can't "try again" like it says – BookwormBoy Aug 16 '22 at 12:59
  • Or is it a different exit? – BookwormBoy Aug 16 '22 at 12:59
  • user3121023 you fixed my problem. Thanks! – BookwormBoy Aug 16 '22 at 13:05

0 Answers0