Zero and setprecision

I have a program that needs to output the input with a setprecision of 2 but when it outputs 0, it has to be "0", not "0.00"

How will I go about that?

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void PrintAllSales(double sales[][SLSPRDT])
{
    cout << fixed << showpoint << setprecision(2) << endl;
    
    cout << "ALL SALES \n";
    WriteLine('=', 63);
    cout << "Product #" << setw(6) << "|" << setw(15) << "1 " << "|" << setw(15) << "2 " << "|"
         << setw(15) << "3 " << "| \n";
    WriteLine('-', 63);
    
    for(int i = 0; i < SLSPRNS; i++)
    {
        cout << "Salesperson " << i+1 << " |";
        for(int j = 0; j < SLSPRDT; j++)
        {
            cout << setw(14) << sales[i][j] << " |";
        }
        cout << endl;
    }
}


And here is how it comes out:

1
2
3
4
ALL SALES 
===============================================================
Product #     |             1 |             2 |             3 | 
Salesperson 1 |       2000.00 |          0.00 |        299.00 |
Salesperson 2 |          0.00 |       2543.98 |         95.99 |
Salesperson 3 |          0.00 |          0.00 |         19.99 |
Salesperson 4 |          0.00 |       3854.75 |          0.00 |


But it should be like this:

1
2
3
4
ALL SALES 
===============================================================
Product #     |             1 |             2 |             3 | 
Salesperson 1 |       2000.00 |             0 |        299.00 |
Salesperson 2 |             0 |       2543.98 |         95.99 |
Salesperson 3 |             0 |             0 |         19.99 |
Salesperson 4 |             0 |       3854.75 |             0 |


Any help would be appreciate it.
you can use if statements to check if there is a number that is not zero, set the decimal places to two, else just print 0
Where would I do that? Because I tried that but it doesn't seem to work. Any other ideas?

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
void PrintAllSales(double sales[][SLSPRDT])
{
    cout << fixed << showpoint << setprecision(2) << endl;
    
    cout << "ALL SALES \n";
    WriteLine('=', 63);
    cout << "Product #" << setw(6) << "|" << setw(15) << "1 " << "|" << setw(15) << "2 " << "|"
         << setw(15) << "3 " << "| \n";
    WriteLine('-', 63);
    
    for(int i = 0; i < SLSPRNS; i++)
    {
        cout << "Salesperson " << i+1 << " |";
        for(int j = 0; j < SLSPRDT; j++)
        {
            if(sales[i][j] != 0)
            {
                setprecision(0);
            }
            else
            {
                setprecision(2);
            }
            cout << setw(14) << sales[i][j] << " |";
        }
        cout << endl;
    }
}


any other ideas?
set the precision if it is not zero. otherwise just directly output '0'
Can you show me some code of what you mean because I'm trying that but since it is an array I don't see how I can output just '0' if everything else would be setprecision of 2.

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
void PrintAllSales(double sales[][SLSPRDT])
{    
    cout << "ALL SALES \n";
    WriteLine('=', 63);
    cout << "Product #" << setw(6) << "|" << setw(15) << "1 " << "|" << setw(15) << "2 " << "|"
         << setw(15) << "3 " << "| \n";
    WriteLine('-', 63);
    
    for(int i = 0; i < SLSPRNS; i++)
    {
        cout << "Salesperson " << i+1 << " |";
        for(int j = 0; j < SLSPRDT; j++)
        {
            if(sales[i][j] != 0)
            {
                cout << fixed << showpoint << setprecision(2);
            }
            else
            {
                cout << '0';
            }
            cout << setw(14) << sales[i][j] << " |";
        }
        cout << endl;
    }
}


And this is what I get:


1
2
3
4
Product #     |             1 |             2 |             3 | 
Salesperson 1 |          1.00 |0          0.00 |          2.00 |
Salesperson 2 |          1.00 |          2.00 |          3.00 |
Salesperson 3 |          1.00 |          2.00 |          3.00 |
Salesperson 4 |          1.00 |          2.00 |          3.00 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 for(int i = 0; i < SLSPRNS; i++)
    {
        cout << "Salesperson " << i+1 << " |";
        for(int j = 0; j < SLSPRDT; j++)
        {
          if(sales[i][j] != 0)
            {
                cout << fixed << showpoint << setprecision(2) << setw(14) << sales[i][j] << " |";
            }
            else
            {
                cout <<  setprecision(0) << setw(14) <<  '0'  << " |";
            }
        }
        cout << endl;
    }







maybe that.
Last edited on
That did it. The trick was to cout the array inside the if statement, I see now.

Thank you.
You are welcome :)
Topic archived. No new replies allowed.