Thread: simple Q on whitespace..

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    simple Q on whitespace..

    Code:
    char name[30];
    
    printf("Enter name: ");
    fflush(stdin);
    fgets(name,30,stdin);
    what I want is to save a string (with whitespaces) to "name" because "name"s from the user will be stored in another array (array of strings that is)

    can anyone help me on achieving that? i'm sorry i'm still a bit confused to using this fgets and stuff.. >.<

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fgets will read whitespaces.

    fflush(stdin) is by no means guaranteed to do what I think you expect it to do - it is undefined (in the C standard) for files that are not OUTPUT files - stdin is a INPUT file. You can find an alternative to this in the FAQ ("How do I clear up unwanted input" or some such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    thanks for the FAQ.

    i've seen some "solutions" around:

    -i've read others saying about an fpurge(stdin)..
    -using fgets then sscanf..

    which do your recommend? or can you recommend other ways?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm now a bit confused as to what you are actually trying to achieve. Aside from fflush(), I don't see anything wrong with your corrent code.

    If you are discussing alternatives to the fflush() solution, yes, if you want to "mix" scanf() with fgets(), an option is to use fgets() for all data reads, and use sscanf() to convert data to numbers when needed (or split strings into multiple smaller strings, but sscanf() isn't ideal for that - as sscanf() is hard to make do the right thing with regards to size of the fields and such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    this is a larger block of my code

    Code:
    int pos;
    char name[30];
    
    printf("Enter name: ");
    fgets(name,30,stdin); //sorry.. maxChar is #define-d as 30 in my code.. :)
    printf("Enter position: ");
    scanf("%d",&pos);
    that code is actually a case in a switch function.. what I can't think of is that when I compiled it in a separate file.. it worked out well.. but inside the "case", it prints out something like this:

    Code:
    Enter name: Enter position:
    what is happening? O_O
    Last edited by iunah; 01-16-2009 at 12:55 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right. My guess is that you have a scanf() somewhere before this case-statement, which leaves a newline in the input buffer. fgets() then sees the newline and says "Done".

    You need to clear the input buffer (or use fgets() + sscanf() instead of scanf).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So fgets and scanf are perfectly fine functions, but they work in different ways. scanf always start by throwing away whitespace, and leaves whitespace behind. fgets doesn't throw away whitespace (allowing it to pick up blank lines or whatever) but cleans up behind itself and doesn't leave whitespace.

    This means a scanf followed by an fgets will do bad things unless you take care of the intervening whitespace, or more commonly, just use one or the other.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    yes, it does have a scanf before that case-statement.. but it only asks for an int "choice" on the "menu".. how can I use fgets + sscanf for that? or rather, how can I clean up the whitespace that scanf did?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought of the scanf(), followed immediately by the fgets(), so I tried it - but fgets() always waited for input. (and yes, there was a newline in the keyboard buffer)

    Of course, that was using the new-fangled Turbo C/C++ ver. 1.01, <cough, cough >, so <ROFL>

    getchar(), will pull one newline off the income stream.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    at last, I found a way to dump that line on scanf.. ( i used a separate function just for that dumping)

    thanks for all the help!

    until then I sleep..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM