Weird table issue.

For some reason this table produces several unwanted zeroes in between the values. How do I get rid of the zeroes?

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
//Inches to centimeters
#include <iostream>
#include <iomanip>
using namespace std;

double Centimeters(double);

int main()
{
    // Set Precision.  I think the issue might be here.
    cout << setprecision(12) << fixed << showpoint;              
    // Chart
    cout << "Inches" << setw(12) << right << "Centimeters";
    cout << "\n------------------------------------------\n";
    // Loop.
    for (double Inches = 0; Inches <= 12; Inches++)
    {
    cout << setw(10) << right << Inches << setw(12) << right << Centimeters(Inches)
    << endl;
    }
    return 0;
    }
    //return as Celsius.
    double Centimeters(double Inches)
    {
    double Centimeters = (Inches * 2.54);

    return Centimeters;
    }

Here is the DOS window that the program outputs.

Inches Centimeters
------------------------------------------
0.0000000000000.000000000000
1.0000000000002.540000000000
2.0000000000005.080000000000
3.0000000000007.620000000000
4.00000000000010.160000000000
5.00000000000012.700000000000
6.00000000000015.240000000000
7.00000000000017.780000000000
8.00000000000020.320000000000
9.00000000000022.860000000000
10.00000000000025.400000000000
11.00000000000027.940000000000
12.00000000000030.480000000000

Process returned 0 (0x0)   execution time : 0.345 s
Press any key to continue.



Yeah, setprecision is the issue. You are setting the precision of your doubles to 12 decimal places. You probably only want 2, or maybe 3 right? So just change setprecsion(12) to setprecision(2).
// Set Precision. I think the issue might be here.
cout << setprecision(12) << fixed << showpoint;

Bingo. What did you expect this would do?
Anyway, see here: http://www.cplusplus.com/reference/iostream/manipulators/
Topic archived. No new replies allowed.