Reading in an int followed by a string

This is a discussion on Reading in an int followed by a string within the C Programming forums, part of the General Programming Boards category; Why is it that the following code: printf("? "); scanf("%d", &n); printf("? "); gets(str); skip the gets(str) step? My guess ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    6

    Reading in an int followed by a string

    Why is it that the following code:

    printf("? ");
    scanf("%d", &n);
    printf("? ");
    gets(str);

    skip the gets(str) step?

    My guess is that the carriage return is not stored into n and still in the stdin, so that when gets(str) is called, the carriage return is encountered. Am I correct?

    Is there anyway to solve this anomaly besides switching the positions of the code or use gets() and atoi?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    I believe you are correct, the CR has not been read yet and carries into the second read.

    Add after the first read a call to flush the input buffer:
    fflush(stdin);

    That should do it for you.

    Walt

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Salem
    Someone else needs to read the FAQ as well then
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    The FAQ does state for the C standard fflush(stdin); behavior is undefined but it also states As usual, there are some exceptions, check your compiler's documentation. The original question was
    Why is it that the following code:
    . . .
    skip the gets(str) step?
    not is there a better way which the response from Salem answered.

    In Borland, fflush(stdin) works as early as Turbo 1.0. If they removed it's funtionality, I am unaware of it, although I doubt it.

    Maybe a better answer using scanf would be
    sscanf("%d%c", &num, &dummy);
    with the %c to read off the CR, but the typed in character string is critical. It in fact would be better to use fgets() routine as implied. I personally dislike scanf() because of its ideosyncracies.

    Walt

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>In Borland, fflush(stdin) works as early as Turbo 1.0.
    The problem is you have no idea what compiler the OP has, nor any other reader of this forum who might be learning from your response.

    In a C forum, it is better to promote the user of standard tools for the job, and leave the compiler specific answers to the relevant forums. This doesn't always work (like now), but you should try and keep it so.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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