C++ Tutorial Basic Input/Output Bug

Hi,
there is a bug either in the script or answer,

if i run a compiler i get 154 as an answer
(Enter price: 22,25 Enter quantity: 7 Total price: 154),

not 155,75.


best regards
Last edited on
closed account (2UD8vCM9)
Really? No code? What do you expect us to help you with?

Edit: Actually I see your issue but in the future don't post such a descriptionless problem with no code like this.

The problem is you are using an int instead of a float. Ints can only be whole numbers, so 7*22.25 changes to 7*22 which = 154

Use floats instead of ints
Last edited on
We need more information to help. You've posted the input, expected output, and the actual output that you're receiving. Those are definitely useful, but you need to post your code also.

When you post code, please put it in code tags. Enter the code, then look at the right side of the input window. Highlight the code and click the <> button at right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// stringstreams
#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;

}





and on the right side of display stands 155,75

http://www.cplusplus.com/doc/tutorial/basic_io/

Last edited on
closed account (2UD8vCM9)
Please mark as answered if your question was answered. :)
I assume you are inputting the value as 22,25 but you need to input it as 22.25
Yes, so is it. Thank you..
Topic archived. No new replies allowed.