Help Displaying Words saved to variable

a
Last edited on
First, you're doing way too much work than you need to do. You need not use the stringstream() function at all. cin would accomplish what you need to do. More on that later.

The first thing you do is use getline(cin, mystr) That's all you need to do! The console will take whatever the user enters until the return key is pressed and store it in mystr, but you try to convert it to an int (which you stated you already feel is wrong). Is NameProduct an int? That's like asking if your name is a number. Absolutely not! I'd suggest renaming your mystr variable to NameProduct, getting rid of your current int NameProduct, and you'll be good to go.

Then you use getline to try to get the cost of the product. This is unnecessary. Use:
1
2
double CostProduct;
cin >> CostProduct;

and you're good to go. The second line of this code states. "The next number entered by the user will be a double, store it in this double variable as a result." The same process can be used for the OriginalSale variable.
Last edited on
When you say "change your mystr variable to NameProduct are you suggesting something like this:
1
2
3
cout << "What's the name of the product?" << endl;
getline (cin, NameProduct);
cin >> NameProduct;

? I not too sure on that. Where would initializing the variable come in?

a
Last edited on
getline(cin, Mystr); is already requesting input from the users and storing it into "mystr", and i think you're wanting to store that in my product not the mystr.

1
2
getline(cin, mystr);
cin >> mystrl;

Is not needed, geline will do what you want it to do in itself.
Last edited on
Still just getting loads of errors now, I'm just going downhill. If someone could type the full thing out the way they're suggesting, that'd help out a lot. That way I can see the finished product and see what I've messed up on and why.
This code is almost ready to go. Remove line 16. It is redundant (also, it wouldn't accept a name with spaces in it anyway). And lastly, your only other errors are because you've been misspelling variable names. There's nothing else actually wrong with the code. Read those compiler errors!
Last edited on
Awesome, Awesome, Awesome! @Keene Thank-You so much for your help.
I've completely misunderstood the use of getline(cin, mystr) So basically mystr is the variable name correct? The getline takes what the person types and saves it with cin to variable name mystr, Correct?
No you only need getline. It will store what the user inputs into the console.
Here's the difference between the two.
Let's say you type "Dole Peaches" into the console.
getline(cin, mystr); will store "Dole Peaches" in mystr. cin >> mystr; will only store "Dole" into mystr. It's either one or the other, and it's typically never a good idea to mix the two.
Topic archived. No new replies allowed.