Thread: endless loop for scanf - plz help

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Question endless loop for scanf - plz help

    im trying to write a function that gets a number (int) from the user using scanf but for some reason the following code results in an endless loop. The point of the loop is to make sure the user enters a number and not a string:

    Code:
    //Function makes sure the user entered a valid entry
    //and returns it
    int Get_Line_Ammount()
    {
    	int amm_of_lines;
    	int return_value;
    
    	printf("How many lines do you want to store?: ");
    	return_value = scanf("\n%d", &amm_of_lines);
    
    	while (return_value == 0)
    	{
    		printf ("\nYou did not enter a valid number, Please re-enter: ");
    		return_value = scanf("%d", &amm_of_lines);
    	}
    
    	return amm_of_lines;
    }
    pleazzzz, and thank you.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Hi,
    The following line is your problem..both outside of the loop, and inside of it.

    return_value = scanf("\n%d", &amm_of_lines);

    you dont really need the "return_value =" part. replace other places you have 'return_value' with 'amm_of_lines'


    hope that helps,
    Deep




    Quote Originally Posted by owi_just
    im trying to write a function that gets a number (int) from the user using scanf but for some reason the following code results in an endless loop. The point of the loop is to make sure the user enters a number and not a string:

    Code:
    //Function makes sure the user entered a valid entry
    //and returns it
    int Get_Line_Ammount()
    {
    	int amm_of_lines;
    	int return_value;
    
    	printf("How many lines do you want to store?: ");
    	return_value = scanf("\n%d", &amm_of_lines);
    
    	while (return_value == 0)
    	{
    		printf ("\nYou did not enter a valid number, Please re-enter: ");
    		return_value = scanf("%d", &amm_of_lines);
    	}
    
    	return amm_of_lines;
    }
    pleazzzz, and thank you.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The problem is, whatever caused scanf() to return 0 the first time around is left on the input stream to cause it to return 0 all over again.

    Which is why scanf() on it's own is pretty useless for input.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      if ( sscanf( buff, "%d", &myint ) == 1 ) {
        /* success */
      } else {
        /* fail */
      }
    }
    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.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    still dont work

    thanx for the try but again it created an enless loop.
    any suggesstions?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Posting your latest code helps.
    Remember, don't try and mix fgets() with scanf()
    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.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    I was thinking to keep it simple but then again, Im pretty new. See the following...
    -Deep

    Code:
    int Get_Line_Ammount()
    {
    	int amm_of_lines;
    
                    // this is your priming read
    	printf("How many lines do you want to store?: ");
    	scanf("%d", &amm_of_lines);
    
    	while (amm_of_lines== 0)
    	{
    		// code here (for you to fill in) is similar to your priming read
    	}
    
    	return amm_of_lines;
    }

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    sorry salem, i didnt mean the code that you gave me, i meant the one that DEEP posted, i didnt see yours at first

    anyway, thanks for the tip it worked but in the specific exersize that im doing i cant declare a string, i have to do without it.

    any suggestions?

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Quote Originally Posted by owi_just
    sorry salem, i didnt mean the code that you gave me, i meant the one that DEEP posted, i didnt see yours at first

    Actually, my code above worked perfectly fine for me..

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    i must be doing something else wrong then, maybe its outside the function

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Quote Originally Posted by owi_just
    i must be doing something else wrong then, maybe its outside the function
    possibly. feel free to post your full code, assuming it is not too long. In the meantime, about this function.. test it out on its own. Open a new .c file, and type the following...

    Code:
    #include <stdio.h>
    
    int main()
    {
      int test=0;
    
      printf("How many lines do you want to store?: ");
      scanf("%d", &test);
    
      while (test == 0)
      {
        printf("\n");
        printf("Cant enter zero, try again:  ");                
        scanf("%d", &test);
      }
    
    
      printf("\nThe number of lines is:  %d\n", test);
      return 0;
    }
    Compile that and run it. Hope now its starting to make some sense.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Compile that and run it. Hope now its starting to make some sense.
    Did you try typing in "hello" rather than 1234 ?
    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.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    this is the copy/past of the output screen after i entered "hello" instead of a number:



    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:
    Cant enter zero, try again:

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Excellent - now you know why it's a bad answer
    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.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Ah apologies for my "bad answer". Im new, and was just trying to help.

    Salem, maybe you can provide the correct way to do it (without declaring a string)?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Ah apologies for my "bad answer". Im new, and was just trying to help.
    Which is good, true understanding comes when you can explain it to others.
    But be aware that your answers have to be solid otherwise people will be all over you

    > Salem, maybe you can provide the correct way to do it
    A bullet-proof reading of an integer using only scanf?
    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. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 08:15 PM