stringstream
Sep 4, 2008 at 7:24pm UTC
Hi. By following the tutorials, i cannot understand the advantage that stringstream provides.
The example code at the tutorial about stringstream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price=0;
int quantity=0;
cout << "Enter price: " ;
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: " ;
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
But i think that the following code does the same work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
using namespace std;
int main ()
{
float price=0;
int quantity=0;
cout << "Enter price: " ;
cin>>price;
cout << "Enter quantity: " ;
cin>>quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
The second code is simplier and it seems it does the same work. What is the difference? And I actually does not understand the stringstream() and getline() expressions too. Can anybody explain it Or give another basic example about this issue? Thanks a lot.
Sep 4, 2008 at 7:37pm UTC
Last edited on Sep 4, 2008 at 7:38pm UTC
Topic archived. No new replies allowed.