Thread: Save long string of characters AND spaces

  1. #1
    Registered User
    Join Date
    Sep 2006

    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
    #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;
        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?"


  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    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
    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


    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