Monday, July 27, 2009

What's wrtong with my C++ program?

I can't get it to covert the celsius to Fahrenheit. Help!!





#include %26lt;iostream%26gt; // defines the cout object


#include %26lt;iomanip%26gt;


using namespace std;





int main()





{


int Fahrenheit;


int Celsius;


const double Celsius_to_Fahrenheit = (9/5)*Celsius -32;





cout %26lt;%26lt; "\t===========\n";


cout %26lt;%26lt; "\tConversions\n";


cout %26lt;%26lt; "\t===========\n\n\n";





cout %26lt;%26lt; "Celsius \tFahrenheit\n";


cout %26lt;%26lt; "========\t========\n\n";





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision (1);





for ( Fahrenheit = 0; Fahrenheit %26lt;= 100; Fahrenheit += 4)


cout %26lt;%26lt; setw(5) %26lt;%26lt; Fahrenheit %26lt;%26lt; " \t\t" %26lt;%26lt; Celsius_to_Fahrenheit%26lt;%26lt; setw(5) %26lt;%26lt; endl;





cout %26lt;%26lt; endl%26lt;%26lt; endl;








cout %26lt;%26lt;endl;


system ("pause");


return 0;


}

What's wrtong with my C++ program?
Well I think you're problem probably is that your Fahrenheit is the only one that changes in the output right? Well you really can't change a constant value either, so what you'd probably have to do is this.





for ( Fahrenheit = 0; Fahrenheit %26lt;= 100; Fahrenheit += 4)


cout %26lt;%26lt; setw(5) %26lt;%26lt; Fahrenheit %26lt;%26lt; " \t\t" %26lt;%26lt; 17.7+(5/9)*Fahrenheit%26lt;%26lt; setw(5) %26lt;%26lt; endl;





In other words you need to actually use Fahrenheit in the equation inside of the for loop. If you want to keep the equation the other way the for loop has to use Celsius as the index (Celsius = 0; Celsius%26lt;= 100; Celsius += 1).
Reply:Your problem is this: Celsius_to_Fahrenheit . You define it as a constant, with the var Celsius, which is "undefined" at compile time. And since it is a constant, it never changes.





Change it to a function or just insert the equation into your out put statement. Oh, and if you are converting C to F, make sur e that your for loop indicates that.
Reply:why you ask this question on Yahoo computer %26amp; internet, when there are load of C++ forums, just google them, and they will help you with your assignments.
Reply:K'


C takes statements one at a time. To define a const double as a function of celsius doesn't work - it takes the current gibberish stored in Celsius, does the math, and stores it in Celsius_to_Fahrenheit. To fix this up you need to keep track of whats going on and do the calculation within the loop and cout line.





From your loop statement it seems you want to display Fahrenheit tempertures from 0 - 100 in multiples of 4. (0,4,8,16...).


Instead you want to do Celsius to Fahreinheit 0 -100 in multiple of 4:





for ( Celsius = 0; Celsius%26lt;= 100; Celsius += 4)


cout %26lt;%26lt; setw(5) %26lt;%26lt; Celsius /*Display Celsius Temp */%26lt;%26lt; " \t\t" (9/5)*Celsius -32 /*Display Converted Number from Celsius to Farenheit */%26lt;%26lt; %26lt;%26lt; setw(5) %26lt;%26lt; endl;





You don't need to have Farenheit or a conversion as data storage points at all.


No comments:

Post a Comment