Thread: accepting space into an array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    accepting space into an array

    I am trying to allow the user to enter spaces in between words into my array, but I cannot figure out how to do it.

    I am using the scanf function, I have tried the following code as it should accept whitespaces but it doesn't accept any characters.

    I know this is probably quite an easy question but I can't find the answer in the tutorials or my C programming book, as it says this will work.

    Code:
    scanf("%c", surname);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your current use of scanf is only reading one character. Consider using fgets instead. There's a FAQ on this topic (look above the blue bar, click FAQ).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I am trying to allow the user to enter spaces in between words into my array
    you have char array with a string in it and want to allow the user to add more spaces between words?
    or you want the user to enter a string with spaces and store it in the array?

    Code:
    scanf("%c", surname);
    is surname a pointer?

    did you mean %s

  4. #4
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    If you want to use scanf then try something like
    Code:
    scanf("%20[^\n]", surname); //where 20 would be the max length
    But using fgets would probably be the best option.
    Last edited by SirNot; 11-15-2006 at 04:14 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The above code should read exactly one character from stdin. To read more, you can put a number before the c, for however many characters you wish to read.

    There are also functions gets() and fgets() for reading in newline terminating strings. gets() reads from stdin up to the first newline (as generated by pressing enter) character and replaces the newline with \0. But it can read more characters than your array can store, leading to buffer overflow errors. If can cause your program to crash or currupt your other data. fgets() is simmilar, but does does not replace the newline with \0. fgets() requires you to pass stdin as a file pointer to read from standard input. It avoids the problem of buffer overflow by an additional parramiter of the max number of characters to read.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by SirNot
    If you want to use scanf then try something like
    Code:
    scanf("%20[^\n]", surname); //where 20 would be the max length
    But using fgets would probably be the best option.
    That won't work. Strange thing about scanf is that it treats all whitespace the same. So your code is assentially the same as scanf("%20s", surname);.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by King Mir
    Quote Originally Posted by SirNot
    If you want to use scanf then try something like
    Code:
    scanf("%20[^\n]", surname); //where 20 would be the max length
    But using fgets would probably be the best option.
    That won't work. Strange thing about scanf is that it treats all whitespace the same. So your code is assentially the same as scanf("%20s", surname);.That won't work. Strange thing about scanf is that it treats all whitespace the same. So your code is assentially the same as scanf("%20s", surname);.
    No, look up that [ directive.

    (But still prefer fgets.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    King, please change your name to: "Ignore me, I'm always wrong." Or at least put it in your sig as a warning to all those who don't know any better.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    quzah, there's no reason to be like that, you can just agree that he is incorrect and move on.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your opinion, not mine. You be you, I'll be me. I'm not trying to change you, so ........ off with your trying to change me. If you don't like my comments, put me on ignore.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I'm not trying to change you, I just like the forum to be a reasonaly amicable place, and calling someone "always wrong" is not something I feel helps this forum. I just like a friendly enviroment, rather than one where if someone makes a mistake they are jumped on. It's just common courtesy. Point out his mistake (as Dave_Sinkula did) and leave it there.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by manutd
    calling someone "always wrong" is not something I feel helps this forum.
    Yes, but they are always wrong. Thus, I'm going to be sure and tell everyone that, because people that don't know better, will assume they're correct. Which, they never are.

    Just like many teachers who teach incorrectly. It's fine if you read their advice, if you keep in mind that they're usually wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Dave_Sinkula
    No, look up that [ directive.

    (But still prefer fgets.)
    Yeah, I swear I read somewhere that [^\n] will not work though. I can't find it now, so you're probably right.
    Last edited by King Mir; 11-15-2006 at 11:55 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by King Mir
    Yeah, I swear I read somewhere that [^\n] will not work though. I can't find it now, so you're probably right.
    While you're looking, there's another directive that is not space-delimited.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 08:42 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM