[prog] functions

Wolf Rising wolfrising at gmail.com
Sat Dec 18 18:02:35 EST 2004


Hi, 

I'm trying to do a program that converts farhenheit to celsius and
celsius to farhenheit, I looked
up the formula for the conversion. The first function seems to work
correctly, the second
function which I set up exactly the same, is returning negative
numbers. Would anyone happen
to see what I'm doing incorrectly or did I just get lucky that one
function worked to begin with? :-)

Thanks

#include <iostream>
using namespace std;

//declare functions
float cel(float );
float fahr(float );

int main() {
	
	//define variables
	int c = 0;
	int f = 32;
	
	//header
	cout << "Fahrenheit to Celsius temperatures:\n";
	cout << "Celsius" << "\t\t" << "Fahrenheit\n";
	
	
	//loop to 100 
    while (c <= 100 )
	{
		//call function
		cout << c << "\t\t\t\t" << fahr(c) << endl;
		
		//increment counter
		++c;
	}
	
	cout << endl;
	
	//header
	cout << "Celsius to Fahrenheit temperatures:\n";
	cout<< "Fahrenheit" << "\t\t" << "Celsius\n";
	
	
    //loop to 212
    while ( f <= 212)
	{
		//call function
		cout << f << "\t\t\t\t" << cel( f ) << endl;
		
		//increment counter
		++f;
	}
	
	cout << endl;
	return 0;
}

//define function
float cel(float temp)
{
	return (5.0 / 9.0 *  temp - 32);
}

//define function
float fahr(float temp)
{
	return (9.0 / 5.0 * temp + 32);
}


More information about the Programming mailing list