two decimals

how do i do the outputs to be in two decimal places. i tried setprecision but its not working. can you check what iam doing wrong please. its sales so our professor wants it to be like $9000.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

float getSales(string div);
void findHighest(float sw, float nw, float se, float ne);

int main()
{
   float sw, nw, se, ne;
   sw = getSales("Southwest");
   nw = getSales("Northwest");
   se = getSales("Southeast");
   ne = getSales("Northeast");
   cout << endl;
   findHighest(sw, nw, se, ne);
   cout << endl;
   return 0;
}

float getSales(string div)
{
   float sales;
    while(true) {
       cout << "Enter the sales figure for the " <<setprecision(6)<< div << " division: ";
       cin >> sales;
       if(sales > 0) {
           break;
       }
       cout << "The sales figure must be a positive dollar amount." << endl;
   }
   return sales;
}

void findHighest(float sw, float nw, float se, float ne)
{
   float highestSales = 0;
   if(sw > nw && sw > se && sw > ne)
   {
       cout << "The Southwest division had the highest sales this quarter." << endl;
       highestSales = sw;
   }
   else if(nw > se && nw > ne)
   {
       cout << "The Northwest division had the highest sales this quarter." << endl;
       highestSales = nw;
   }
   else if(se > ne)
   {
       cout << "The Southeast division had the highest sales this quarter." << endl;
       highestSales = se;
   }
   else
   {
       cout << "The Northeast division had the highest sales this quarter." << endl;
       highestSales = ne;
   }
  
   cout << "The division's sales were $" << highestSales << endl;
}
1. Why do you set the precision in getSales()? Why not in line 60? That is not intuitive.

2. Look at example in http://www.cplusplus.com/reference/iomanip/setprecision/
which leads to http://www.cplusplus.com/reference/ios/fixed/
That is your real issue.
Hello poonamp6792,

After you read the links that keskiverto suggested what I would try is to put this line after line 11. std::cout << std::fixed << std::showpoint << std::setprecision(2);. With time and experience I found that this line only needs to be done once and will affect every "cout" statement the follows until it is changes. You will find that "std::fixed" will use decimal numbers as opposed to "scientific notation". The "std::showpoint" will print the ".00" in an answer. and it looks like "std::setprecision(2)" you already have some understand of. All three parts will give you what you want.

Right now the "std::setprecision(2)" as you are using it deals with both the lhs and rhs of the decimal point plus the decimal point as I remember it.

While I am here "double"s are preferred over "float"s. The precision of a "double" is greater then a "float" and will store the number better, but not always correctly. This has more to do with how the computer stores the floating point number rather than the type of the variable.

On line 11 you should initialize all your variables. If your compiler is C++11 compliant you can simply us empty {}s and this will set the variable to in this case 0.0. Even though the function calls will give these variables a value in this case it is best not to have a garbage value for these variables.

Line 4 it is best not to use this line in a program. Some day this WILL get you in trouble. It may seem easy now, but you are missing the opportunity to learn what is in the standard name space while it is easy.

Hope that helps,

Andy
Topic archived. No new replies allowed.