C++ Fixed Point Notation

There are three types of Food at a Stadium.

1) Hotdog costs $5
2) Drink costs $4
3) Nacho Hats costs $12
Write a program that asks how many of each type of food were sold, then displays the amount of income generated from food sales. Format your dollar amount in fixed-point notation, with two places of precision, and be sure to the decimal point it always displayed.

I need help with the above question and how to use set precision?

Thank you,

Kirk
Fixed point precision is just saying that the first n digits of a number belong to the right side of the decimal point.

Your program expects n==2. So:

1234 means 12.34
76 means 0.76
500 means 5.00

To get the whole part, use division: 500 / 100 = 5.
To get the 'fractional' part, use remainder: 567 % 100 = 67.

To make C++ display things properly, you'll need the setw() and setfill() manipulators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Fill empty spaces with zeros.
// (you only need to call this one once, somewhere at the beginning of your program)
cout << setfill('0');

// Print a number as 0.00
cout << whole_part << "." << setw(2) << fractional_part << "\n";

// Print the number 0.00
cout << 0 << "." << setw(2) << 0 << "\n";

// Print the number 50.84
cout << 50 << "." << setw(2) << 84 << "\n";

// Print $5.00
cout << "$" << 5 << "." << setw(2) << 0 << "\n";

Hope this helps.

[edit] Oh, don't forget to #include <iomanip>
Last edited on
Topic archived. No new replies allowed.