Ive been following this tutorial but dont really get the point made in the stringstream section… Basic Input/Output
Am I right in thinking all the code is doing (right at the bottom of the page) is saving the getline variable as a string called mystr then converting it with stringstream into a numerical variable called price.
Then the same for quantity.
Why not just save the numbers as variables and multiply those. I dont get the point in saving it as a string and then turning it into a number.
I’m not exactly sure why you wouldn’t just accept integer only input, instead of messing around with stringstream, but it does have a purpose when a user specified string can be safely and easily converted to another type.
You’ll probably find uses for it later on. Sometimes I use a traditional string with a char array, and then get user input in C++ by using the cin.getline() function. [code]#include
using namespace std;
int main()
{
char str[200];
cin.getline(str, 200);
cout << str << endl;
return 0;
}[/code]But its up to you how you do it. Sometimes its helpful to have C++ classes like stringstream to do some of the heavy work of checking bounds, and keeping everything safe.