Thread: how to make stringstream take in spaces as input?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    how to make stringstream take in spaces as input?

    Code:
    int main()
    {
    	string input = "abc def";
    	stringstream buffer(input);
    	char temp;
    	int i = 0;
    	char something[20];
    
    	while(buffer >> temp)
    	{
    		something[i] = temp;
    		i++;
    	}
    }
    So I have written that code, but the values that variable temp is getting are only the characters a, b, c, d, e, f. I want it to take a value of ' ' (space) as well. Is there any way to do that?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    temp = buffer.get()

    In your case: while((temp=buffer.get()), buffer)
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    Could you explain what this second usage of 'buffer' is supposed to do?
    I mean while(temp=buffer.get()), buffer)

    Because, before you edited the post I used (completely randomly, without knowledge how to use this function):
    while(buffer.get(temp))

    and it worked.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Comma is an operator. If used that way it says: Do what's on the left side, returns what's on the right side.

    Remember that "buffer >> temp" returns buffer.

    EDIT: Yes, i forgot! Your way is better. Nicely done!
    Devoted my life to programming...

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Code:
    int main()
    {
        std::string input = "abc def";
        std::stringstream buffer(input);
        char temp;
        int i = 0;
        char something[20];
        buffer >> std::noskipws; // don't skip whitespace
    
        while(buffer >> temp)
        {
            something[i] = temp;
            i++;
        }
        something[i] = 0;
        std::cout << "Something = \"" << something << "\"\n";
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. fgets - user input including spaces
    By MethodMan in forum C Programming
    Replies: 24
    Last Post: 03-12-2004, 07:36 PM
  4. string input...with white spaces?
    By Pureghetto in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 01:52 PM
  5. Having Spaces in Input
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2002, 11:54 AM

Tags for this Thread