locked
Why is this starting a unlimited loop? RRS feed

  • Question

  • do
    		{
    			printf("\nGeburtsJAHR (JJJJ) : ");
    			if (scanf("%i", &(eingabe[groeße].jahr)) == 0)
    			{
    				printf("\nFalsche Datentyp");
    				eingabe[groeße].jahr = 1900;
    			}
    			
    
    		} while (eingabe[groeße].jahr < 1960 || eingabe[groeße].jahr > 2015);

    this is a lil snipped from my code and i dont know why it is an loop when u enter a wrong datatype

    as far as i know scanf returns 0 when the scanned datatype dont match

    so when i enter abc for eingabe[groeße].jahr it is starting the loop

    but shouldnt it just go to the scanf and wait till i enter something?

    now it is u type a wrong type for jahr and it is looping without and  but i want to to wait at the scanf till i enter something again

    Saturday, November 7, 2015 6:25 PM

Answers

  • i dont know why it is an loop when u enter a wrong datatype

    as far as i know scanf returns 0 when the scanned datatype dont match

    so when i enter abc for eingabe[groeße].jahr it is starting the loop

    but shouldnt it just go to the scanf and wait till i enter something?

    >as far as i know scanf returns 0 when the scanned datatype dont match

    Yes in this case where you only have one extraction to perform. But generally the return is
    the number of fields successfully converted and assigned. So if you have multiple conversion
    specifiers such as "%c%d%s" if the first succeeds but one or both of the others fail then the
    return would be 1 or 2. It would be 3 if they were all successful.

    >shouldnt it just go to the scanf and wait till i enter something?

    If you have %i and you enter "abc" then the conversion will fail and the "abc" will still
    be in the input stream. The next scanf with %i still finds the "abc", fails and leaves the
    "abc" in the stream, ad infinitum.

    - Wayne

    • Proposed as answer by Barry Wang Monday, November 9, 2015 6:55 AM
    • Marked as answer by Shu 2017 Monday, November 16, 2015 9:47 AM
    Saturday, November 7, 2015 10:27 PM

All replies

  • Try this preliminary solution:

    . . .

    if( scanf( "%i", &eingabe[groeße].jahr ) == 0 ) { printf( "\nFalsche Datentyp" ); scanf( "%*s" ); continue; }

    . . .




    • Edited by Viorel_MVP Saturday, November 7, 2015 7:33 PM
    Saturday, November 7, 2015 7:32 PM
  • i dont know why it is an loop when u enter a wrong datatype

    as far as i know scanf returns 0 when the scanned datatype dont match

    so when i enter abc for eingabe[groeße].jahr it is starting the loop

    but shouldnt it just go to the scanf and wait till i enter something?

    >as far as i know scanf returns 0 when the scanned datatype dont match

    Yes in this case where you only have one extraction to perform. But generally the return is
    the number of fields successfully converted and assigned. So if you have multiple conversion
    specifiers such as "%c%d%s" if the first succeeds but one or both of the others fail then the
    return would be 1 or 2. It would be 3 if they were all successful.

    >shouldnt it just go to the scanf and wait till i enter something?

    If you have %i and you enter "abc" then the conversion will fail and the "abc" will still
    be in the input stream. The next scanf with %i still finds the "abc", fails and leaves the
    "abc" in the stream, ad infinitum.

    - Wayne

    • Proposed as answer by Barry Wang Monday, November 9, 2015 6:55 AM
    • Marked as answer by Shu 2017 Monday, November 16, 2015 9:47 AM
    Saturday, November 7, 2015 10:27 PM