A few things are still unclear to me regarding the use or string streams since I have not used them much. For example, shouldn't you be using a stringstream instance instead of either an istringstream or an ostringstream?
Anyways, at least I see in the code segment you present that 'iss' is not initialized to contain anything in particular since the constructor has no argument. Since it is not initialized, you are getting garbage out of it. You should try to include the string in your constructor, like this:
istringstream iss(c);
Now the stream will start with the content provided by the user. Well, I think it will... let me know!
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, my question today is:
I can't understand something:
string c;
int a, b;
char ch;
getline(cin, c);
istringstream iss;
//user input for c: 13/7
iss >> a >> ch >> b;
But, when I try the output, like this:
cout << a << '\t' << b << endl;
the result is something completely different than 13 and 7. Do you know why
is that? I know that if I write in my program:
c = "13/7";
iss >> a >> ch >> b;
cout << a << '\t' << b << endl;
the result will be correct. Is there a solution for getting numbers from
getline(cin, c)?
Greetings, Franjo
If I'm not mistaken your way of using getline is wrong. Check http://www.cppreference.com/cppio_details.html#getline.
Try
char c[100];
cin.getline(c,100);
...
iss >> a >> ch >> b;
cout << a << '\t' << b << endl;
upcase
Franjo:
A few things are still unclear to me regarding the use or string streams since I have not used them much. For example, shouldn't you be using a stringstream instance instead of either an istringstream or an ostringstream?
Anyways, at least I see in the code segment you present that 'iss' is not initialized to contain anything in particular since the constructor has no argument. Since it is not initialized, you are getting garbage out of it. You should try to include the string in your constructor, like this:
istringstream iss(c);
Now the stream will start with the content provided by the user. Well, I think it will... let me know!
qWake
Thanks for answers, upcase and qWake. qWake, you were right. Iss must be bound to string and must be declared AFTER the user input.
int a, b;
char ch;
string c;
getline(cin, c);
istringstream iss(c);//That is correct
iss >> a >> ch >> b;
string c;
istringstream iss(c);//In that case, iss will extract empty string
getline(cin, c);
iss >> a >> ch >> b;
Thanks again guys,
Franjo