Sunday, July 12, 2009

Help with C++ Program ?

Program takes celsius degree


// and converts it to fahrenhite


// in increments of 5





#include %26lt;iostream%26gt;





using std::cout; // program uses cout


using std::cin; // program uses cin


using std::endl; // program uses endl





#include %26lt;iomanip%26gt;





using std::setw;





int main()


{


int c, f, even, odd; // declares variables





cout %26lt;%26lt; "Input data in intervals of 5" %26lt;%26lt; endl;


cout %26lt;%26lt; "Start from 0" %26lt;%26lt; endl;





for (f = 0; f %26lt; 100; f++)


// Counter to loop a hundred times


{


f = f*5;


c = (.5555556)*(f-32);





if( c %26amp; 0x00000001 )


{


c = even;


}


else


c = odd;





cout%26lt;%26lt; setw( 5 ) %26lt;%26lt; "Odd"%26lt;%26lt; setw( 5 ) %26lt;%26lt; "Even" %26lt;%26lt; endl;


cout%26lt;%26lt; setw( 5 ) %26lt;%26lt; odd %26lt;%26lt; setw( 5 ) %26lt;%26lt; even %26lt;%26lt; endl;





// conversion Formula set to c


cout %26lt;%26lt; "The Celsius equivalent is: " %26lt;%26lt; c;


// printout statement


if(c %26lt; 35)


// Checks to see

Help with C++ Program ?
..yikes, what are you trying to do!?


is it a program that takes C, converts it to F, and THEN adds 5 , or is it a program that displays the C temperature in F, in intervals of 5?


the latter sounds more reasonable, I guess...but ONE of your problems is that you are using your loop controls as variables to convert the temp, so after the firs time in your first for loop the variable goes to 6, the second time to 30, and then to 150!! don't use the F variable as loop control if it is for conversion, unless you are going to restore back its value after the conversion, in which case makes no sense...you should just use ANOTHER variable for loop control
Reply:Your temperatures should be floats.
Reply:sorry, i'm not entirely sure about what youre trying to do...


but to convert a temp C to F i would do this





int main (void)


{


double c, f;





cout%26lt;%26lt;"Enter a temperature in degrees Celsius: ";


cin%26gt;%26gt;c;


cout%26lt;%26lt;endl;





f = 9.0/5.0*c + 32;





cout%26lt;%26lt;c%26lt;%26lt;" degrees Celsius is equal to "%26lt;%26lt;f%26lt;%26lt;" degrees Fahrenheit.";





return 0;


}








If you're trying to make a scale of temperatures, something like


0C = 32F


5C = 41F


10C = 50F


15C = 59F


...one hundred times, make a loop that


-changes C to F


-prints value of C and F


-adds 5 to C


something like





double c = 0, f;





for(int loop = 0; loop %26lt; 100; loop++)


{


f = 9.0/5.0*c + 32;


cout%26lt;%26lt;c%26lt;%26lt;"C = "%26lt;%26lt;f%26lt;%26lt;"F"%26lt;%26lt;endl;


c += 5;


}





hope this helps you, good luck with the programming.


No comments:

Post a Comment