Thread: Can a String Have Spaces In It?

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question Can a String Have Spaces In It?

    Can they? When I have the user input a string that has spaces in it, my program goes crazy. Is this because strings can't have spaces, or is it something wrong with my program?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A string can have spaces in it, but a lot of string-handling functions stop when they encounter a space.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The overloaded >> operator is designed to stop at whitespace. You might look up:

    istream::getline()
    string::getline()

    Both will read in tabs spaces and stop a newline (or other delineator if supplied).

  4. #4
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Can anything be done about that? All I'm doing with the string is putting it into a vector and displaying on the screen.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
    	vector<string> stuffs;
    	string vectorBuf;
    
    
    	// Reads input into "buffer" and pushes it onto vector
    	for(int i = 0; getline(cin, vectorBuf, '\n'); )
    	{
    		stuffs.push_back(vectorBuf);
    	}
    
    	// Iterates through the vector spitting out it's contents
    	for(vector<string>::iterator iter = stuffs.begin();
    		iter < stuffs.end(); iter++)
    	{
    		cout << *iter << endl;
    	}
    }
    Be done about what?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Can anything be done about that?
    Yes - he just showed you. When you use the << operator, you're actually calling a function (even though you don't really see this) that stops when it encounters a space. If you use the getline functions like he showed you, they will accept spaces, and only stop once they get to a newline character. You can look through www.cppreference.com to find other functions and their behavior.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    getline(cin , instring , '\n');



    where cin is where4 your getting your input from
    where instring is your string variable
    and '\n' is your delimiter.

    for use of this you must include <string>
    header, if you are currently unaware
    you can define instring like this


    string instring;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM