Thread: Strange problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    Strange problem

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char name[10], pet[20];
    
    	scanf( "%s", name );
    	printf( "Hello %s!\n", name );
    
    	gets( pet );
    	puts( pet );
    
    	return 0;
    }
    Why the the gets statement doesn't work? Even if i change it with fgets( pet, sizeof( pet ), stdin ); it still doesn't work.
    It's so strange...

    Thanks in advance
    Loading.....
    ( Trying to be a good C Programmer )

  2. #2
    can't log
    Guest
    It's possible that gets() reads the stdin buffer, before you even get the chance to press anything. So do some error checking on gets() and puts().

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I think i understand you. I made some changes, here are the results which are strange and i would like some explanaions if you don't mind:

    if i write:

    scanf( "%s ",name ); // note the space

    gets does work, but before the printf statement. It asks me to type the string of name[], then the string to pet[] and then it prints back the name and then the pet. However i thought it should first print the name, then ask for pet and the print the pet. Why?

    if i write:

    scanf( "%s", name );
    getchar();

    I works perfectly. However i think that the results should be the same with what i wrote above. Why it works fine now but not and before? If i write fflush(stdin) instead of getchar() it will also work fine. But why. Fflush won't flush the variable name?

    If i write:

    scanf( "%s", name );
    printf( "Hello %s!\n", name );
    getchar()
    gets( pet );
    puts( pet );

    It works fine and i understand why,

    And instead of that getchar() if i write fflush( stdin ), it will also work fine.

    Can you tell me why it works like that in my first 2 if conditions?
    Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Hmmmmm.... ok. Many thanks.
    You'r code is perfect, but for just a so little code i don't prefer it.
    But there aren't any other solutions, are they?. Here i think that C disadvantages. What do you think?

    Many thanks again.
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by money?
    But there aren't any other solutions, are they?. Here i think that C disadvantages. What do you think?

    Many thanks again.
    The easiest solution in your original code is to change the scanf() to fgets() since scanf does not play nice with your later input.

    Also change the gets() to fgets() because gets is a disaster waiting to happen.

    And if the print does not happen when you think it should, add the fflush(stdout) where necessary
    Last edited by WaltP; 07-13-2003 at 01:47 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM