Whats wrong with my program
Oct 31, 2010 at 8:39pm UTC
i dont why but i am not getting this program to work ( the second for statement)
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
void main()
{
float celsius, kelvin, fahrenheit;
ofstream output ("e:result14.dat" );
cout<< "name" <<endl;
output<< "name" <<endl;
cout<< setw(15) << "Celsius" << setw(15) << "Kelvin" <<setw(15) << "Fahrenheit" <<endl;
output<< setw(15) << "Celsius" << setw(15) << "Kelvin" <<setw(15) << "Fahrenheit" <<endl;
for (celsius=-100.0;celsius<=235.0;celsius=celsius+4)
{
kelvin=celsius+273.15;
fahrenheit=(9/5.0)*celsius +32.0;
cout << setw(15) << celsius << setw(15)<< kelvin << setw(15) << fahrenheit << endl;
output << setw(15) << celsius << setw(15)<< kelvin << setw(15) << fahrenheit << endl;
}
cout<< setw(15) << "Fahrenheit" << setw(15) << "Celsius" <<setw(15) << "kelvin" <<endl;
output<< setw(15) << "Fahrenheit" << setw(15) << "Celsius" <<setw(15) << "Kelvin" <<endl;
for (fahrenheit=-310.0;fahrenheit<=-400.0;fahrenheit=fahrenheit+7)
{
celsius=(5/9.0)*fahrenheit - 32.0;
kelvin=celsius+273.15;
cout << setw(15) << fahrenheit << setw(15)<< celsius << setw(15) << kelvin << endl;
output << setw(15) << fahrenheit << setw(15)<< celsius << setw(15) << kelvin << endl;
}
system("PAUSE" );
return 0;
}
and thats my output:
name
Celsius Kelvin Fahrenheit
-100 173.15 -148
-96 177.15 -140.8
-92 181.15 -133.6
-88 185.15 -126.4
-84 189.15 -119.2
-80 193.15 -112
-76 197.15 -104.8
-72 201.15 -97.6
-68 205.15 -90.4
-64 209.15 -83.2
-60 213.15 -76
-56 217.15 -68.8
-52 221.15 -61.6
-48 225.15 -54.4
-44 229.15 -47.2
-40 233.15 -40
-36 237.15 -32.8
-32 241.15 -25.6
-28 245.15 -18.4
-24 249.15 -11.2
-20 253.15 -4
-16 257.15 3.2
-12 261.15 10.4
-8 265.15 17.6
-4 269.15 24.8
0 273.15 32
4 277.15 39.2
8 281.15 46.4
12 285.15 53.6
16 289.15 60.8
20 293.15 68
24 297.15 75.2
28 301.15 82.4
32 305.15 89.6
36 309.15 96.8
40 313.15 104
44 317.15 111.2
48 321.15 118.4
52 325.15 125.6
56 329.15 132.8
60 333.15 140
64 337.15 147.2
68 341.15 154.4
72 345.15 161.6
76 349.15 168.8
80 353.15 176
84 357.15 183.2
88 361.15 190.4
92 365.15 197.6
96 369.15 204.8
100 373.15 212
104 377.15 219.2
108 381.15 226.4
112 385.15 233.6
116 389.15 240.8
120 393.15 248
124 397.15 255.2
128 401.15 262.4
132 405.15 269.6
136 409.15 276.8
140 413.15 284
144 417.15 291.2
148 421.15 298.4
152 425.15 305.6
156 429.15 312.8
160 433.15 320
164 437.15 327.2
168 441.15 334.4
172 445.15 341.6
176 449.15 348.8
180 453.15 356
184 457.15 363.2
188 461.15 370.4
192 465.15 377.6
196 469.15 384.8
200 473.15 392
204 477.15 399.2
208 481.15 406.4
212 485.15 413.6
216 489.15 420.8
220 493.15 428
224 497.15 435.2
228 501.15 442.4
232 505.15 449.6
Fahrenheit Celsius Kelvin
see i cant get my numbers to print for the second for statement
Oct 31, 2010 at 9:19pm UTC
What's with the output <<
instead of cout <<
?
Oct 31, 2010 at 9:23pm UTC
what??
Oct 31, 2010 at 9:31pm UTC
@buld.......what jamesmk is saying is that you should not use output<< its not part of C++ syntax.....instead use only cout<<
Oct 31, 2010 at 9:31pm UTC
Nevermind.
Oct 31, 2010 at 9:32pm UTC
but without the output how can i print my work?
Oct 31, 2010 at 9:32pm UTC
Output is a file stream he declared above.
Oct 31, 2010 at 9:40pm UTC
so now why my second for statement is not showing my numbers ...whats is the problem?
Oct 31, 2010 at 9:51pm UTC
for (fahrenheit=-310.0;fahrenheit<=-400.0;fahrenheit=fahrenheit+7)
-400 is < -310
So it won't ever go through the 2nd for loop ever.
Oct 31, 2010 at 9:54pm UTC
Either loose the '-' on the 400, or change the '<=' to '>='.
Oct 31, 2010 at 9:54pm UTC
What jamesmk said.
Oct 31, 2010 at 10:02pm UTC
yes.....Thanks !! now last question how can i set the results to show 4 decimals places.....
because when i
setprecision (4)
only the last 2 columns gets 4 decimals but the first doesn't....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
for (celsius=-100.0;celsius<=235.0;celsius=celsius+4.0)
{
cout<<setprecision (4);
kelvin=celsius+273.15;
fahrenheit=(9/5.0)*celsius +32.0;
cout << setw(15) << celsius << setw(15)<< kelvin << setw(15) << fahrenheit << endl;
output << setw(15) << celsius << setw(15)<< kelvin << setw(15) << fahrenheit << endl;
}
cout<< setw(15) << "Fahrenheit" << setw(15) << "Celsius" <<setw(15) << "kelvin" <<endl;
output<< setw(15) << "Fahrenheit" << setw(15) << "Celsius" <<setw(15) << "Kelvin" <<endl;
for (fahrenheit=-400.0;fahrenheit<=-310.0;fahrenheit=fahrenheit+7)
{
cout<<setprecision (4);
celsius=(5/9.0)*fahrenheit - 32.0;
kelvin=celsius+273.15;
cout << setw(15) << fahrenheit << setw(15)<< celsius << setw(15) << kelvin << endl;
output << setw(15) << fahrenheit << setw(15)<< celsius << setw(15) << kelvin << endl;
}
output:
Celsius Kelvin Fahrenheit
-100 173.15 -148
-96 177.15 -140.8
-92 181.15 -133.6
-88 185.15 -126.4
-84 189.15 -119.2
-80 193.15 -112
-76 197.15 -104.8
-72 201.15 -97.6
-68 205.15 -90.4
-64 209.15 -83.2
-60 213.15 -76
-56 217.15 -68.8
-52 221.15 -61.6
-48 225.15 -54.4
-44 229.15 -47.2
-40 233.15 -40
-36 237.15 -32.8
-32 241.15 -25.6
-28 245.15 -18.4
-24 249.15 -11.2
-20 253.15 -4
-16 257.15 3.2
-12 261.15 10.4
-8 265.15 17.6
-4 269.15 24.8
0 273.15 32
4 277.15 39.2
8 281.15 46.4
12 285.15 53.6
16 289.15 60.8
20 293.15 68
24 297.15 75.2
28 301.15 82.4
32 305.15 89.6
36 309.15 96.8
40 313.15 104
44 317.15 111.2
48 321.15 118.4
52 325.15 125.6
56 329.15 132.8
60 333.15 140
64 337.15 147.2
68 341.15 154.4
72 345.15 161.6
76 349.15 168.8
80 353.15 176
84 357.15 183.2
88 361.15 190.4
92 365.15 197.6
96 369.15 204.8
100 373.15 212
104 377.15 219.2
108 381.15 226.4
112 385.15 233.6
116 389.15 240.8
120 393.15 248
124 397.15 255.2
128 401.15 262.4
132 405.15 269.6
136 409.15 276.8
140 413.15 284
144 417.15 291.2
148 421.15 298.4
152 425.15 305.6
156 429.15 312.8
160 433.15 320
164 437.15 327.2
168 441.15 334.4
172 445.15 341.6
176 449.15 348.8
180 453.15 356
184 457.15 363.2
188 461.15 370.4
192 465.15 377.6
196 469.15 384.8
200 473.15 392
204 477.15 399.2
208 481.15 406.4
212 485.15 413.6
216 489.15 420.8
220 493.15 428
224 497.15 435.2
228 501.15 442.4
232 505.15 449.6
Fahrenheit Celsius Kelvin
-400 -254.222 18.9278
-393 -250.333 22.8167
-386 -246.444 26.7056
-379 -242.556 30.5944
-372 -238.667 34.4833
-365 -234.778 38.3722
-358 -230.889 42.2611
-351 -227 46.15
-344 -223.111 50.0389
-337 -219.222 53.9278
-330 -215.333 57.8167
-323 -211.444 61.7056
-316 -207.556 65.5944
i need the first column to show 4 decimal places too any idea how??
Oct 31, 2010 at 10:09pm UTC
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
Topic archived. No new replies allowed.