Save long string of characters AND spaces

This is a discussion on Save long string of characters AND spaces within the C++ Programming forums, part of the General Programming Boards category; How can I have a variable or string of, for example, "hello, how have you been today?" I used Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    Post Save long string of characters AND spaces

    How can I have a variable or string of, for example, "hello, how have you been today?" I used
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     
        system("color 02"); 
        cout<<"Hello World!\n";
        string test;
        cin>> test;
        cout<< test;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    to test this, but it always comes out with the string is only "hello" instead of "hello, how have you been today?"

    Thanks.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    First, don't use system() calls when you don't need to. The first system call can be replaced with routines written for your Operating System. The second can be replaced with
    Code:
    cin.ignore(); 
    cin.get();
    You're not including <string> which is where the string object is contained, you're lucky that your <iostream> implementation seems to include it.

    Now your real problem is that you're trying to read the string with the >> operator. This operator stops on whitespace like a space, a tab, or a newline character. You're going to want to use the getline function provided by the <string> library.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    Post

    Thanks! it worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-17-2005, 07:01 PM
  2. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  3. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  4. How to count characters, but not spaces?
    By RedZippo in forum C++ Programming
    Replies: 17
    Last Post: 03-29-2004, 04:09 PM
  5. string input...with white spaces?
    By Pureghetto in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 12:52 PM

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