I found the easiest way to do this was simply making the default case look like:

Code:

default:
printf( "Bad input, try again" );
continue;

Nothing wrong with this, is there?

I don't know how your current program looks like but I would guess if you input something like "abc" you get 3 error messages because continue will jump to the beginning of the do-while-loop and the while-loop which consumes any character left in the input stream won't be executed.

And what does the getchar(); do after the scanf(); ? It is needed for the program to operate correctly, but I still do not understand what it is doing.

If you input something, the characters are stored in the input stream. scanf(%d) consumes as many characters as it can interpret as a decimal number and leaves all additional characters in the input stream. E.g. if you enter "1" followed by <Enter>, the input stream looks like "1\n". scanf() consumes the "1" but leaves the "\n" in the buffer. getchar() gets the next character in the input stream (in this case "\n"). Thus many programmers (especially beginners) use the combination scanf()/getchar() to avoid problems later in the program because of unwanted characters (especially "\n") still left in the input stream.

IMHO a while-loop as Salem suggested is more robust, because it removes all characters, not just the next one.

Your getchar() will remove ONE char from the input buffer - but only ONE. That may be perfectly fine if the user is reasonably good - but many user's are GENIUSES at giving bad input.

If you enter several bad chars (not numbers), for your input, you should see multiple displays of "Bad input, try again".

Using the while loop that Salem showed you in his earlier post, will remove ALL the bad char's, so you will have just ONE display of "Bad input, try again". The while loop could go right into the same #4 switch option, as the "Bad input, try again", printf() line of code.

Try it and see - that's one of the best things about programming - you can quickly try out a lot of different code versions and see what does what.

I am just learning C, I understand now what I would need to do, but I don't feel it necesary to correct it that far... I Will fix it later when I actually make a game that uses it. Thanks go to everybody who helped me with this, the feel of this forum is good, full of helpful and knowledgable people... I am going to take on a project soon enough, the graal relay. I will post a million questions later about it

The word "goto" is why I got interested in computer programming. My mother was taking night classes to learn Pascal and BASIC, and I would see her writing BASIC code on our Commodore 64. I could understand most of it, like PRINT and IF-THEN, that all made sense. But some of her programs contained GOTO, and I was intrigued by this new word (I imagined it was pronounced "goh-toh") and I decided I would teach myself BASIC so that I could understand this "GOTO" command, which was very mysterious to me.

Of course, C64 BASIC lacked any structured loops other than FOR-NEXT, and so GOTO was required in many cases.