facing problem with Buffer

This is a discussion on facing problem with Buffer within the C Programming forums, part of the General Programming Boards category; Hi, I have written program to take a string from the user using fgets() but its not taking the string, ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    post your code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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