Thread: Why does the array not accept blankspaces?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Why does the array not accept blankspaces?

    When you enter a word inside a char x[30] array, for example Max, it's working, but if you enter Max Maximus, it automaticly fills the NEXT cin. How do you fix it?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Operator >> only reads input up to the whitespace. To read everything until [Enter] is pressed use getline.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Beacause a char array cannot accept strings with spaces. The only way is to use a string array with getline or if you use a char array make it an array of char pointers.

    Code:
    char *pData[ 2 ] = { "Hello World!", "C++ Is Cool!" };
    Double Helix STL

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    swgh, this is an array of C-style strings, not a single string. As you can see both individual strings in it do contain spaces without any problem.

    (The only character that a C-style string cannot contain is '\0'.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Ah ok sorry anon I think i mis-understood
    Double Helix STL

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    So, how is it done? Do you have to use the pointer method?
    getline >> x?
    getline x?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More like:
    Code:
    cin.getline(x, 30);
    Though you may want to consider making x a std::string instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then if it's a std::string, it would be better to use std::getline instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    62
    It just skips the cin when the program is run when you use getline!?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Please post your example. There are minor differences that could cause your problem that depend on the code you are doing.

    Using getline will read in multiple words even if there are spaces between them. Using operator>> will read in only a single word.

    Using C++ string (i.e. std::string) is smart but has no effect on whether spaces will be read in or not.

    Do you have a book or a favorite reference website? It should describe how to call getline.

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. How to make char array accept spaces
    By izah in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 02:11 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM