Thread: facing problem with Buffer

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43

    facing problem with Buffer

    Hi,
    I have written program to take a string from the user using fgets() but its not taking the string, and also later in the program i have used scanf to take an integer value from the user even that not requesting user to enter the value...what may be the problem could anyone explain me how shall i proceed?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    post your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Code:
    while(1)
    {
                printf("\n\n\n**********************************************************\n"
    
    		"****************WELCOME TO STUDENTS DB********************\n"
    			"**********************************************************\n");
    
    		printf("\n\n\n\t1. Add a New Entry\n"
    
    			"\t2. Delete an Entry\n"
    
    			"\t3. Update an Entry\n"
    
    			"\t4. Search\n"
    
    			"\t5. List\n"
    
    			"\t6. Exit\n");
    
    		printf("\n\nPlease Enter ur Choice: ");
    
    		scanf("%d",&choice);
    
    		switch(choice)
    
    		{
    
    			case 1:
    
    				stud_add(&obj);
    
    				break;
    
    			case 2:
    
    				delete(&obj);
    
    				break;
    
    			case 3:
    
    				update(&obj);
    
    				break;
    
    			case 4:
    
    				search(obj);
    
    				break;
    
    			case 5:
    
    				list(obj);
    
    				break;
    
    			case 6:
     
    				_exit(0);
    
    				break;
    
    			default:
    
    				printf("The Choice entered was INVALID\n");
    
    		}
    
    	}

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    In the above code when i enter some invalid numbers or any char after few iterations it goes into an infinite loop.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Replace your scanf call with something better, or if you insist on using it, something like this:
    Code:
    while( scanf("%d", &foo ) != 1 )
        printf("Try again:\n");

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d",&choice);
    If you type in 'a', then two things happen (or rather, two things don't happen)
    1. The value 'a' isn't converted
    2. The value 'a' isn't removed from the input stream.

    So you go round the loop again, and still 'a' is there, and still it fails (ad infinitum)

    Code:
    char buff[BUFSIZ];
    if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      if ( sscanf( buff, "%d", &choice ) == 1 ) {
        // success!!!
      } else {
        // bah, user typed in garbage
      }
    }
    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. facing problem in cmopiling the code
    By ishwarverma in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 09:22 PM
  2. facing problem in understanding this....
    By enggabhinandan in forum C Programming
    Replies: 10
    Last Post: 10-25-2006, 05:30 AM
  3. Buffer Overrun Project, Problem Entering NULL in to stream
    By Peter5897 in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2006, 05:12 PM
  4. Popen problem with stdout buffer
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 05-26-2004, 03:00 AM
  5. memory allocation problem....help..
    By CyC|OpS in forum C Programming
    Replies: 8
    Last Post: 10-18-2002, 09:26 AM