Hi all.
I've read allot about streams but I'm wondering if anyone can tell me why cin >> stringstream doesn't work?
I'm getting there but my understanding has some floors in it.
eg.
stringstream Mystr;
cin >> Mystr;
Thanks so much.
Hi all.
I've read allot about streams but I'm wondering if anyone can tell me why cin >> stringstream doesn't work?
I'm getting there but my understanding has some floors in it.
eg.
stringstream Mystr;
cin >> Mystr;
Thanks so much.
Because there's no overloaded operator>> that does this for you. It's simple enough to write, though you could just as easily reverse the operation and dump cin into Mystr:
Mystr << cin.rdbuf();
That's probably why the overloaded operator doesn't exist, it's unnecessary.
Aha.. thanks
But when I use
Mystr << cin.rdbuf()
The program stays open. Really, what I'm trying to do is, in a command line program, entering a sentence and having that recorded in a variable and then displaying that back to me?
I'm not sure how to do it?
Thought the stringstream object would let me do that but I'm not sure how.
Well if you want to store the sentance in a variable use a string not a stringstream. Your code would look like this
string line;
getline(cin, line); // gets the sentence from the command line and place it in line
cout << "You Entered: " << line << endl;
Hi Nathan.. Thanks very much for that.. You too deceptikon..
Thats perfect for what I need.
Was wondering though, and this is an additional question really, how would I store a sentence read from text into a variable. I know know if if is input from a keyboard I would use the above process but if I have a sentence from somewhere else how would I go about it? If I put the sentence in a variable I can use it later on for something else.
Thansk for the help so far.
What do you mean by getting the sentence from somewhere else? Do you mean like getting it from a file or passing the variable to the program using the command line? If you put the sentence in a variable you can use it in your progrma anywhere you want to as long as that variable is visable to the code block you are in.
What I meant was, in the above example the string 'line' receives a line from user input using getline().
If I have a program read a sentence from a file for example, and I want to store that sentence in a variable, how do I do it?
You can use the same method. Instead of using cin
you would use the file stream you create.
ifstream fin("testfile.txt"); //create ifstream object to read the file
string line;
vector<string> fileContents;
while(getline(fin, line))
fileContents.push_back(line);
As you can see from the above example fin
is the file stream to read from the file. fileContents
is a vector that will store each line of the file. The while loop uses the return value from getline to know if a line was read from the file. If the getline operation fails the loop stops and nothing more is read from the file. Inside the while loop the contents of the variable line are then added to the back of the vector.
If you want a good reference site I would suggest visiting cplusplus.com. They have a lot of tutorials and references. And of course if you have problems come back here and someone should be able to help.
Thanks very much. That works perfectly.
It answers a few questions actually.
Thanks again.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.