0

I'm curious if it's possible to put letters out of a getline function into 2 separate string variables, sorry if it's hard to understand but I'll explain this further.

Let's say I have a 'string x;' variable, I use a 'getline(cin, x)' and then I were to run this and type in for example 'hello world'. Would it be possible to put 'hello' and 'world' into 2 separate string variables?

For example: It will store the first letter keep on adding letters to a variable until it hits a whitespace, then keep on adding letters to the next variable until it reaches another whitespace.

I understand I might have explained this poorly, and if it's not understandable let me know and I'll try explain more.

2 Answers2

1

the stream operator>> functions already handle whitespace formatting for you, the canonical way to handle an unknown number of tokens on each line is to do something like the following:

std::string line;
getline(std::cin, line);

std::istringstream iss(line);
std::string token;
while (iss >> token) {
  // Do something with token
}

If you don't care about what kind of whitespace is used to separate the tokens this can be simplified to

std::string token;
while (std::cin >> token) {
  // Do something with token
}
user657267
  • 20,568
  • 5
  • 58
  • 77
  • Appreciate the effort, I'll have to experiment some with your examples and read up on sstream. – user3546673 Apr 22 '14 at 01:18
  • It's really no different from any other stream, only that it operates on `string`s instead. – user657267 Apr 22 '14 at 01:19
  • std::istringstream iss(line); Isn't completely understandable to me, so I'll have to figure out what that does, the rest is understandable though. – user3546673 Apr 22 '14 at 01:22
  • `std::istringstream iss(line);` creates a new `istringstream` with its content set to that of `line`. – user657267 Apr 22 '14 at 01:23
  • I figured I should probably use a string vector instead of storing it in separate strings. – user3546673 Apr 22 '14 at 01:24
  • Was wondering if you could help me out a bit more. So, if I used your first example and I were to write 'hello world' in the console. Then how would I store these words separately in a vector? How do I make it performing a push_back() for that specific word, then do the same for the rest of the words? – user3546673 Apr 22 '14 at 01:47
  • [Here](http://pastebin.com/sUvNg6sL) are two ways to do this, you might want to avoid the first if you haven't had a look at the relevant parts of the standard library yet. – user657267 Apr 22 '14 at 01:52
  • Alright, it's working the way I wanted it to. There's just 1 bug at the moment, if I only type a single word and then hit enter I get a unhandled exception > memory out of range or something similar. Any way to fix that? Edit: I mean enter, not space. – user3546673 Apr 22 '14 at 02:07
  • You might be doing something invalid with the input in the read loop, post the relevant code on pastebin or somewhere. – user657267 Apr 22 '14 at 02:10
  • For my project to work I'll need the getline to be able to handle only 1 word too. – user3546673 Apr 22 '14 at 02:15
  • Works fine for me, what compiler and version / platform are you using? It's worth mentioning that you don't need to use `getline` unless you specifically need to know when you've read any given line, `while (std::cin >> token)` works just as well. – user657267 Apr 22 '14 at 02:17
  • Not sure what compiler I am using tbh. I'm using Visual studio ultimate 2013 atleast. But, I'll give you some more information on what I'm working on. I'm working on a text based adventure game where everything the player does will be controlled by user input, so every time the user hits enter I would assume that he entered a command. Why I need to use a getline is because the commands will hold different amounts of words. A few examples on commands I have planned: 'walk area' 'use itemname' 'buy itemname quantity' etc. I already have quantity part done to convert the numbers to an int. – user3546673 Apr 22 '14 at 02:27
  • I just built your code in VS 2013 and it ran without a hitch, input was "foobar" – user657267 Apr 22 '14 at 02:39
  • The unhandled exception only happens with the getline. Still trying to figure out if I can get along with everything fine with just using the while (cin >> token) – user3546673 Apr 22 '14 at 02:41
  • Actually, I'm not having that issue any more. I have no clue why I had it and I have no clue why I'm not having it now. Thanks a ton for your help. Is there anywhere else I can reach you incase I have questions? You seem like a cool lad. – user3546673 Apr 22 '14 at 02:42
  • Just post your questions to SO, if I don't answer someone else (probably with more knowledge than me) will. – user657267 Apr 22 '14 at 02:44
0

You need a way to parse the string using a delimiter (in this case a space ' ').

This post has about a thousand different functions that can solve what you are trying to do.

Split a string in C++?

Easiest way to do this if you know how many variables you will need would be to take advantage of the default std::cin implementation of using space as a delimiter:

std::string x1, x2;          // define two strings
std::cin >> x1 >> x2;        // user enters "Hello World"
std::cout << x1 <<" "<< x2;  // x1 = "Hello", x2 = "World"
Community
  • 1
  • 1
Andrew L
  • 1,164
  • 12
  • 20
  • Your code is perfectly understandable, but this won't work with the idea I have. I need to use a getline and then somehow get variables out of it. Thanks though! – user3546673 Apr 22 '14 at 01:16