Thread: Creating menu, problem

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16

    Creating menu, problem

    Hello all coders! I'm trying to creat menu and it almost works perfectly but there is one problem. When user for example inputs char (asfgdahdh) my menu goes in endless loop. Here's the code:

    Code:
    int main (int argc, const char * argv[]) {
     
    	valikko();
    
    return 0;
    }
    
    
    int valikko (void) {
    
    int valinta;
    
    	printf("-------------------\n");
    	printf("2. Start\n");
    	printf("3. Help\n");
    	printf("4. Quit\n");
    	printf("-------------------\n\n");
    		scanf("%d", &valinta);
    		
    		switch (valinta) {
    			case 2: 
    				peli();
    				break;
    			case 3:
    				ohje();
    				break;
    			case 4:
    				break;
    			default:
    				valikko();
    				
    			}
    
    
    }
    Any ideas what could be causing this problem?

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Tehy View Post
    When user for example inputs char (asfgdahdh)

    ...

    Any ideas what could be causing this problem?
    You just said what was causing the problem. You're not entering what you're supposed to be entering. The f in the word scanf stands for "formatted". Formatted input and output. Meaning, it is for reading exactly what you expect in the exact way you tell it to be read.

    If you insist on using scanf, check its return value and handle the situation appropriately.
    Code:
    while( scanf( "%d", &var ) != 1 )
    {
        printf( "Try again> " );
        fflush( stdout );
    }
    Otherwise, if you're open to other suggestions, read the FAQ on how to get a number from the user.


    Quzah.
    Last edited by quzah; 04-13-2007 at 02:20 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > valikko();
    Calling a function recursively is a poor way of implementing a while loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  2. Menu problem.
    By And3rs in forum C++ Programming
    Replies: 25
    Last Post: 10-12-2008, 10:48 AM
  3. menu check problem
    By SuperNewbie in forum Windows Programming
    Replies: 5
    Last Post: 11-22-2003, 06:34 PM
  4. Context Menu cursor problem
    By dWorkVan in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2003, 11:42 AM
  5. pop up menu problem
    By bigtamscot in forum Windows Programming
    Replies: 5
    Last Post: 06-10-2003, 01:46 AM