Thread: scanf is being skipped

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    scanf is being skipped

    This is my first example program writing to files. Right now, I know nothing on the matter. I do know that scanf can be finicky about taking input, but I have no idea why at this point. The problem occurs in the while loop

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int account;
    	char name[ 30 ];
    	double balance;
    	FILE *cfPtr;
    
    	if ( ( cfPtr = fopen( "client.dat", "w" ) ) == NULL )
    		printf( "File could not be opened\n" );
    	else {
    		printf( "Enter the account, name, and balance.\n" );
    		printf( "Enter EOF to end input.\n" );
    		printf( "? " );
    		scanf( "%d%s%lf", &account, name, &balance );
    /* ------------------------------THIS IS WHERE IT LOOPS INFINITELY--------------*/
    		while ( !feof( stdin ) ) {
    			fprintf( cfPtr, "%d %s %.2f\n", 
    				    account, name, balance );
    
    			printf( "? " );
    			scanf( "%d%s%lf", &account, name, &balance );
    		}
    
    		fclose( cfPtr );
    	}
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know how to type end-of-file at your computer? (In Windows, Ctrl-Z; *nix, Ctrl-D.)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    Yeah, I know that, I should have provided my console output
    Code:
     ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 
    [4]+  Stopped                 ./a.out
    eugene@eugene-laptop:~/cfiles$

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you typed in something that doesn't match the format (once you type in something that doesn't match "%d%s%lf", then you're screwed).

    Edit: The easiest way to do so is to try to type in a string that has a space in it.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    ^ditto
    Thanks

    Why is this the case? I'd understand the first calling of scanf being screwed up. By why must every following iteration of scanf be skipped then?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because where is the bad input going to go? It's not going to just mysteriously vanish.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    scanf reads only the input it can translate to the target type. So if you try to read an integer, and someone does not enter an integer, that input will be ignored and remain in the input buffer.
    Next time scanf is called, it sees the input buffer is not empty, and thus does not ask the user to input anything, instead trying to read and convert the data in the buffer. But seeing as it is the same as before, it fails, and the circle continues on endlessly.
    You have to flush the input buffer to get rid of this unwanted behavior. There are examples in the faq.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM
  3. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM