K... I got it to work... thanks for the help
K... I got it to work... thanks for the help
I found the easiest way to do this was simply making the default case look like:
Nothing wrong with this, is there?Code:default: printf( "Bad input, try again" ); continue;
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.
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.
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.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.
IMHO a while-loop as Salem suggested is more robust, because it removes all characters, not just the next one.
Bye, Andreas
my entire program looks like this right now.I haven't encountered any compile or runtime errors... but I invite you to check, as I am not sure what you mean.Code:#include <stdio.h> int main(void) { int input; printf( "1. Play game\n" ); printf( "2. Load game\n" ); printf( "3. Play multiplayer\n" ); printf( "4. Exit\n" ); do{ int ch; input = 0; printf(" Your choice: "); scanf( "%d", &input ); printf("\n"); getchar(); /* There are no other functions for this example. Only the users choice is printed to show it's working properly. */ switch( input) { case 1: //playgame(); printf("1 was chosen"); break; case 2: //loadgame(); printf("2 was chosen"); break; case 3: //playmultiplayer(); printf("3 was chosen"); break; case 4: printf( "Goodbye" ); break; default: printf( "Bad input, try again" ); continue; // is OK to put here, but not needed, since it is last in order } //while((ch=getchar()) != '\n' && ch != EOF); printf("\n\n"); }while(input != 4); return 0; }
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.
Last edited by Adak; 08-27-2012 at 07:22 PM.
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.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}