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. - //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";
-
}
-
3 2320
Your making a (common) mistake of assuming that all your variables are globally visible. You actually have (properly declared and all) three seperate celsius vars, three fahr vars and 3 fsum and csum vars.
Uou should only declare celsius and fahr in the methods you define, and csum and fsum are only neecded in the main method
Your methods shoudl RETURN a value (if you want to use that value), eg -
float ctof() /* Declare a return type */
-
{
-
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";
-
return fahr; /* Send this value back to the caller*/
-
}
-
You can then modify your main code to include something lioke -
if (temp=='F' || temp=='f' || temp=='2')
-
{
-
fsum = fsum + ctof();
-
fcount++;
-
}
-
Your making a (common) mistake of assuming that all your variables are globally visible. You actually have (properly declared and all) three seperate celsius vars, three fahr vars and 3 fsum and csum vars.
Uou should only declare celsius and fahr in the methods you define, and csum and fsum are only neecded in the main method
Your methods shoudl RETURN a value (if you want to use that value), eg -
float ctof() /* Declare a return type */
-
{
-
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";
-
return fahr; /* Send this value back to the caller*/
-
}
-
You can then modify your main code to include something lioke -
if (temp=='F' || temp=='f' || temp=='2')
-
{
-
fsum = fsum + ctof();
-
fcount++;
-
}
-
Works like a charm!!! I agonized over this for quite a while and had given up. Thanks DeMan!!! You ROCK!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ed |
last post by:
I need to know how to pass variables because I want to use the same
function to varify multiple data imput boxes.
Please tell me what's wrong with this code.
<head>
<script...
|
by: Einar |
last post by:
Hi,
I wonder if there is a nice bit twiddling hack to compare a large
number of variables?
If you first store them in an array, you can do:
for (i = 0; i < n; i++) {
if (array != value) {...
|
by: Bryan Green |
last post by:
So I'm working on a project for a C# class I'm taking, where I need to
keep some running totals via static variables. I need three classes
for three different types of objects. The base class and...
|
by: Dooglo |
last post by:
I'm new VB and programming all together, but I'm getting he hang of it.
My question is;
I'm writting a program to figure square feet and yards when the user inputs
"Length in feet and inch (...
|
by: EOZyo |
last post by:
Hi, i'm trying to set pagination for a search i run on my website, i'll
try to explain the easiest i can:
When i click the search button on search.php, data is received and
stored in variables...
|
by: CDMAPoster |
last post by:
About a year ago there was a thread about the use of global variables
in A97:
http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157
Best Practices by Kang...
|
by: misha |
last post by:
Hello.
I was wandering if someone could explain to me (or point to some
manual) the process of mapping the addresses of host variables by DB2.
Especially I would like to know when DB2 decides to...
|
by: Nate |
last post by:
Hello,
I am looking for a method to automatically declare variables in C.
I'm not sure if there is a good way to do this, but I had something
like this in mind...
int i;
for(i = 1; i < 4;...
|
by: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |