Like Tree2Likes

Alternative to goto?

This is a discussion on Alternative to goto? within the C Programming forums, part of the General Programming Boards category; K... I got it to work... thanks for the help...

  1. #16
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    K... I got it to work... thanks for the help

  2. #17
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    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?
    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.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by .:Lyle:. View Post
    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.

    Bye, Andreas

  4. #19
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    my entire program looks like this right now.
    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;
    }
    I haven't encountered any compile or runtime errors... but I invite you to check, as I am not sure what you mean.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #21
    Registered User
    Join Date
    Aug 2012
    Posts
    10
    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

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    ZuK likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternative to ifstream?
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2009, 11:12 AM
  2. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  3. Any alternative?
    By sCandal2 in forum Windows Programming
    Replies: 4
    Last Post: 01-11-2006, 03:30 PM
  4. gets() alternative
    By Mastadex in forum C Programming
    Replies: 5
    Last Post: 12-11-2004, 11:28 PM
  5. alternative to cin
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 06-25-2002, 05:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21