How to use -- scanf to accept space?

This is a discussion on How to use -- scanf to accept space? within the C Programming forums, part of the General Programming Boards category; hello everyone, i'm having some problem here. i'm using scanf to accept input from user, but it does not accept ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Unhappy How to use -- scanf to accept space?

    hello everyone, i'm having some problem here.
    i'm using scanf to accept input from user, but it does not accept empty space, for example :

    First Name:
    Last Name:
    Address:
    Street:
    State:
    Postal Code:
    Phone no.:
    Fax No.:

    and if i key in:

    First Name: janet
    Last Name: jackson
    Address: 123, south 8th st.
    Street: State: --->this 2 thing will print out before i get a chance to input Street: .

    any help would be appreaciated, thank you so much.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Hi,
    If you use scanf for reading input then the default delimiter is space. So if you want give a string which includes space then it is better to use the function char *gets(char *s)

    This will read the input from standard input upto next line or end of file is reached.


    Sebasti...

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Your probably better off using fgets.. gets is susceptable to buffer overflows..

    char *fgets( char *string, int n, FILE *stream );

    string - Storage location for data

    n - Maximum number of characters to read

    stream - Pointer to FILE structure (or stdin for interactive user input)..
    Last edited by foniks munkee; 02-07-2002 at 06:12 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    scanf() can accept lines as input but it's a bit hackish and fgets is safer.
    ( void )scanf( "%[^\n]", message );

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    24
    And if you insists on using scanf- which seem s a bit cumbersome in this case, use scanf(%s %s %s and so on, until covering all white spaces, &string)

    Then you will also get the title of least efficient code of the year.

    BTW- You should also remeber to use fflush after each use of any of the above function, when intending to continue reading from the buffer.
    And if you get no joy from music hall
    Remember there is always alcohol
    And If you get no joy from Gin
    Here is the abyss jump in

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. How to Convert Space in ascii?
    By larkin10 in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 06:57 AM
  3. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 05:59 PM
  4. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 01:30 PM
  5. Quzah's printf and scanf tutorial...
    By quzah in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 03:59 AM

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