scanf me thinks

This is a discussion on scanf me thinks within the C Programming forums, part of the General Programming Boards category; Hey i have a problem with my code and im not 100% sure what it is, what happens is that ...

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    scanf me thinks

    Hey i have a problem with my code and im not 100% sure what it is, what happens is that once the user has entered an invalid input then it starts a endless loop, i dont know if its my bad programming or maybe scanf, ive herd she ( scanf() ) can be tricky at times, im new so be kind plz.

    [tag]
    [code]

    MainMenu()
    {
    printf("Please chose from the options below:\n");
    printf("\n[1] Start");
    printf("\n[2] About");
    printf("\n[3] Help");
    printf("\n[4] Exit\n\n");

    scanf("%d", &Main_Menu_Answer);

    if(Main_Menu_Answer <= 4)
    {
    printf("you said: %d\n", Main_Menu_Answer);
    }
    else
    {
    printf("\ano\n");
    MainMenu();
    }

    }

    [/tag]

    thankz... *hopes the tags work*

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The problem is in your 'else' statement. You don't want to recursively call your MainMenu() function on an invalid input. Try to loop back to your 1st printf statement upon an invalid input.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    the tags are [code] and [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try something more like this to cover for problematic input:
    Code:
    #include <stdio.h>
    
    static int out_of_range ( int val, int lower, int upper )
    {
      return val < lower || upper < val;
    }
    
    static int menu ( void )
    {
      int option = 0;
      
      while ( out_of_range ( option, 1, 4 ) ) {
        printf ( "Please chose from the options below:\n" );
        printf ( "[1] Start\n" );
        printf ( "[2] About\n" );
        printf ( "[3] Help\n" );
        printf ( "[4] Exit\n\n" );
        
        /* Always check the return value of input functions */
        if ( scanf ( " %d", &option ) != 1 ) {
          /* Tidy up */
          int ch;
    
          while ( ( ch = getchar() ) != '\n' && ch != EOF )
            ;
        }
        
        if ( out_of_range ( option, 1, 4 ) )
          printf("Invalid entry, please try again.\n");
      }
      
      return option;
    }
    
    int main ( void )
    {
      switch ( menu() ) {
      case 1:
        printf ( "Starting...\n" );
        break;
      case 2:
        printf ( "Menu Program -- ver. 1.00\n" );
        break;
      case 3:
        printf ( "No help available\n" );
        break;
      case 4:
        printf ( "Have a nice day!\n" );
        break;
      }
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    -
    Join Date
    Feb 2003
    Posts
    47
    Prelude thank you, you fixed up every problem i had, this is sopose to be going towards my major project for school, ill add "Prelude" as a source of help LOL
    cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 08:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 07:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM

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