Hi - This program converts temperature from C to F and F to C and averages the Celsius outputs in one variable and Fahrenheit outputs in another. My professor asked us to modify this program to use functions. After I created functions and got it running, the csum and fsum variables seem to get the wrong values. It would be great if somebody could spot the problem and give me a hint. Thanks.
Code:
//Fahrenheit and Celsius conversion using functions
#include <iostream>
using namespace std;
void ftoc();
void ctof();
void thrdoptn();
int main()
{
float fahr,celsius,fsum=0,csum=0,faverage,caverage;
char temp;
int cnt,num1,fcount=0,ccount=0;
cout <<"\nThis program converts temperature between Fahrenheit and Celsius.\n"
<<"and uses functions to achieve this objective.";
cout<<"\nHow many times do you want to convert temperatures? Enter integer: ";
cin>>num1;
for(cnt=1; cnt<=num1; cnt++)
{
cout<<"\n\nOption 1: Fahrenheit to Celsius - Press C or 1.\n";
cout<<"Option 2: Celsius to Fahrenheit - Press F or 2.\n\n";
cin>>temp;
if (temp=='C' || temp=='c' || temp=='1')
{
ftoc();
ccount++;
csum+=celsius;
}
if (temp=='F' || temp=='f' || temp=='2')
{
ctof();
fsum+=fahr;
fcount++;
}
if (temp!='F' && temp!='f' && temp!='C' && temp!='c' && temp!='1' && temp!='2')
thrdoptn();
}
faverage=fsum/fcount;
caverage=csum/ccount;
cout<<fsum<<endl;
cout<<csum<<endl;
cout<<"\nNumber of times conversion to Fahrenheit requested: "<<fcount<<endl;
cout<<"Number of times conversion to Celsius requested: "<<ccount<<endl;
cout<<"Average of Fahrenheit outputs is "<<faverage<<endl;
cout<<"Average of Celsius outputs is "<<caverage<<"\n\n";
system("PAUSE");
return 0;
}
void ftoc()
{
float fahr,celsius,csum=0;
char temp;
int ccount=0;
cout<<"\nEnter temperature in Fahrenheit: ";
cin>>fahr;
celsius = (fahr-32)*5/9;
cout<<"\nTemperature in Celsius is:";
cout<<celsius <<"\n";
}
void ctof()
{
float fahr,celsius,fsum=0;
char temp;
int fcount=0;
cout<<"\nEnter temperature in Celsius: ";
cin>>celsius;
fahr = 32 + celsius*9/5;
cout<<"\nTemperature in Fahrenheit is: ";
cout<<fahr <<"\n";
}
void thrdoptn()
{
cout<<"\nYour only choices are between Celsius and Fahrenheit. "
<<"\nChoose between F or C.\n";
}
Comment