Stringstream

I've been trying to understand how stringstream works and when I search on the site here the stringstream page is way too much info for me to understand all at once.


this is the example I from the tutorial I was looking at.

This line in particular is what I'm trying to understand more

stringstream(mystr) >> yours.year;

This didn't seem to work at all

yours.year = mystr

I thought the yours.year could be treated like a normal variable.
Do you have to interact with a struct's variables using that stringstream() format and what is the purpose of using getline() instead of cin?


Thinking about this a little, yours.year would be a data type movies_t and mystr is data type string.

So stringstream is how you can write data of one type to a different type?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
  string mystr;

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
Last edited on
I tried modifying another piece of the tutorial and it's broke. I don't understand why it can't be written this way.

This part won't work here

cout << "Enter year: ";
getline (cin,films[n].year);

this part won't work either

cout << "Enter year: ";
cin >> films[n].year);


all that works is

cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_MOVIES 3

struct movies_t {
  string title;
  int year;
} films [N_MOVIES];

void printmovie (movies_t movie);

int main ()
{
  string mystr;
  int n;

  for (n=0; n<N_MOVIES; n++)
  {
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,films[n].year);
  }

  cout << "\nYou have entered these movies:\n";
  for (n=0; n<N_MOVIES; n++)
    printmovie (films[n]);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
1
2
3
4
5
This part won't work here
 
cout << "Enter year: ";
 getline (cin,films[n].year);
  


films[n].year is an integer getline only works with strings.

getline is for strings only:
http://www.cplusplus.com/reference/string/string/getline/

The difference between cin >> myString; and getline(cin, myString); is that the first will only input one word (up until the first whitespace character), while the second will go all the way until a newline character, so you can enter more than one word with the second version.

One thing stringstreams are useful for is when you want to convert numbers to strings or vice-versa.
You can think of stringstreams as kind of like cout and cin, except that they work with strings instead of the standard output/input.
So for instance, this code converts an int to a string:
1
2
3
4
5
int myInt = 5;
ostringstream convert;
convert << myInt; // Kind of like 'cout << myInt;', except 'myInt' goes to
                  // an internal string buffer instead of the screen
string result = convert.str(); // 'result' now contains "5" 

while this converts a string back to an int:
1
2
3
4
5
6
string myString = "123";
istringstream convert(myString);
int result;
convert >> result; // Kind of like 'cin >> result;', except the input is
                   // coming from an internal string buffer, not the user
// 'result' is now equal to 123 

Topic archived. No new replies allowed.