Why does the array not accept blankspaces?

This is a discussion on Why does the array not accept blankspaces? within the C++ Programming forums, part of the General Programming Boards category; When you enter a word inside a char x[30] array, for example Max, it's working, but if you enter Max ...

  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,479
    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!" };
    I'm just trying to be a better person - My Name Is Earl

  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,479
    Ah ok sorry anon I think i mis-understood
    I'm just trying to be a better person - My Name Is Earl

  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
    22,256
    More like:
    Code:
    cin.getline(x, 30);
    Though you may want to consider making x a std::string instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    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,344
    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, 11:33 AM
  2. How to make char array accept spaces
    By izah in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 03:11 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

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