### Help: Output not as nice as I thought

Hi. I want it to output like a table or at least nicely arranged but for some reason it didn't output as I expected it to be.
How should I fix it and make the output look as nice as image 2.

Output image:
Image 1:http://i.imgur.com/DyGI8jH.jpg
Image 2:http://i.imgur.com/uAP9lij.jpg

 //truefinalproject.cpp #include #include using namespace std; double celsius(int); double fahrenheit(int); int choice; int counter = 0; int main() { cout << "Please input your choice based on the following choices available:\n"; cout << "\n1. Celsius to Fahrenheit Converter"; cout << "\n2. Fahrenheit to Celsius Converter\n\n"; do { cin >> choice; switch (choice) { case 1: cout << "Printing 'Celsius to Fahrenheit Converter' table from 1'C-100'C\n"; cout << "_______________________________________________________________\n"; for ( int a = 0; a < 4; ++a) cout << setw(7) << "Celsius" << setw(10) << "Fahrenheit"; for ( int b = 0; b < 25; ++b ) { for ( int c = 0; c <= 75; c += 25 ) cout << setw(5) << b + c << "'C" << setw(9) << setprecision(1) << fixed << fahrenheit(b + c) << "'F "; } cout << setw(62) << 100 << "'C" << setw(9) << fahrenheit(100) << "'F"; ++counter; break; case 2: cout << "Printing 'Fahrenheit to Celsius Converter' table from 32'F-312'F\n"; cout << "________________________________________________________________\n"; for ( int d = 0; d < 4; ++d ) cout << setw(7) << "Fahrenheit" << setw(10) << " Celsius "; for ( int e = 32; e < 77; ++e) { for ( int f = 0; f <= 135; f += 45 ) cout << setw(5) << e + f << "'F" << setw(10) << setprecision(1) << fixed << celsius(e + f) << "'C "; } cout << setw(65) << 212 << "'F" << setw(6) << celsius(212) << "'C"; ++counter; break; default: cout << "You have inputted an invalue choice.\n"; cout << "Please input another choice.\n"; break; } } while (counter < 1); } double fahrenheit (int celsius) { return static_cast (celsius * (9.0/5.0) + 32); } double celsius (int fahrenheit) { return static_cast ((fahrenheit - 32) * (5.0/9.0)); }
2 only looks good because the width of the line is exactly the width of the console window. Print a new line character at the end of each line, and at the end of the program.
cout << '\n';
If this is your code, I think you should have been able to figure it out.
I only changed case 1

 1234567891011121314151617181920 case 1: cout << "Printing 'Celsius to Fahrenheit Converter' table from 1'C-100'C\n"; cout << "_______________________________________________________________\n"; for ( int a = 0; a < 4; ++a) { // added cout << setw(7) << "Celsius" << setw(11) << "Fahrenheit" << " "; // changed } // added cout << endl; // added for ( int b = 0; b < 25; ++b ) { for ( int c = 0; c <= 75; c += 25 ) cout << setw(5) << b + c << "'C" << setw(9) << setprecision(1) << fixed << fahrenheit(b + c) << "'F "; cout << endl; // added } cout << setw(62) << 100 << "'C" << setw(9) << fahrenheit(100) << "'F"; ++counter; break;

 1234567891011121314151617181920212223242526272829 Printing 'Celsius to Fahrenheit Converter' table from 1'C-100'C _______________________________________________________________ Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit 0'C 32.0'F 25'C 77.0'F 50'C 122.0'F 75'C 167.0'F 1'C 33.8'F 26'C 78.8'F 51'C 123.8'F 76'C 168.8'F 2'C 35.6'F 27'C 80.6'F 52'C 125.6'F 77'C 170.6'F 3'C 37.4'F 28'C 82.4'F 53'C 127.4'F 78'C 172.4'F 4'C 39.2'F 29'C 84.2'F 54'C 129.2'F 79'C 174.2'F 5'C 41.0'F 30'C 86.0'F 55'C 131.0'F 80'C 176.0'F 6'C 42.8'F 31'C 87.8'F 56'C 132.8'F 81'C 177.8'F 7'C 44.6'F 32'C 89.6'F 57'C 134.6'F 82'C 179.6'F 8'C 46.4'F 33'C 91.4'F 58'C 136.4'F 83'C 181.4'F 9'C 48.2'F 34'C 93.2'F 59'C 138.2'F 84'C 183.2'F 10'C 50.0'F 35'C 95.0'F 60'C 140.0'F 85'C 185.0'F 11'C 51.8'F 36'C 96.8'F 61'C 141.8'F 86'C 186.8'F 12'C 53.6'F 37'C 98.6'F 62'C 143.6'F 87'C 188.6'F 13'C 55.4'F 38'C 100.4'F 63'C 145.4'F 88'C 190.4'F 14'C 57.2'F 39'C 102.2'F 64'C 147.2'F 89'C 192.2'F 15'C 59.0'F 40'C 104.0'F 65'C 149.0'F 90'C 194.0'F 16'C 60.8'F 41'C 105.8'F 66'C 150.8'F 91'C 195.8'F 17'C 62.6'F 42'C 107.6'F 67'C 152.6'F 92'C 197.6'F 18'C 64.4'F 43'C 109.4'F 68'C 154.4'F 93'C 199.4'F 19'C 66.2'F 44'C 111.2'F 69'C 156.2'F 94'C 201.2'F 20'C 68.0'F 45'C 113.0'F 70'C 158.0'F 95'C 203.0'F 21'C 69.8'F 46'C 114.8'F 71'C 159.8'F 96'C 204.8'F 22'C 71.6'F 47'C 116.6'F 72'C 161.6'F 97'C 206.6'F 23'C 73.4'F 48'C 118.4'F 73'C 163.4'F 98'C 208.4'F 24'C 75.2'F 49'C 120.2'F 74'C 165.2'F 99'C 210.2'F 100'C 212.0'F
Last edited on
Topic archived. No new replies allowed.