473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Variables keep getting wrong values

20
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.

Expand|Select|Wrap|Line Numbers
  1. //Fahrenheit and Celsius conversion using functions
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. void ftoc();
  7. void ctof();
  8. void thrdoptn();
  9.  
  10. int main()
  11.  
  12.     float fahr,celsius,fsum=0,csum=0,faverage,caverage;
  13.     char temp;
  14.     int cnt,num1,fcount=0,ccount=0;
  15.  
  16.     cout <<"\nThis program converts temperature between Fahrenheit and Celsius.\n"
  17.     <<"and uses functions to achieve this objective.";
  18.  
  19.     cout<<"\nHow many times do you want to convert temperatures? Enter integer:  ";
  20.     cin>>num1;
  21.  
  22.     for(cnt=1; cnt<=num1; cnt++)
  23.  
  24.     {
  25.          cout<<"\n\nOption 1: Fahrenheit to Celsius - Press C or 1.\n";
  26.          cout<<"Option 2: Celsius to Fahrenheit - Press F or 2.\n\n";
  27.          cin>>temp;
  28.  
  29.                if (temp=='C' || temp=='c' || temp=='1')
  30.                {
  31.                 ftoc();
  32.                 ccount++;
  33.                 csum+=celsius;
  34.                }
  35.  
  36.                     if (temp=='F' || temp=='f' || temp=='2')
  37.                     {
  38.                      ctof();
  39.                      fsum+=fahr;
  40.                      fcount++;
  41.                     }     
  42.  
  43.                             if (temp!='F' && temp!='f' && temp!='C' && temp!='c' && temp!='1' && temp!='2')
  44.                             thrdoptn();
  45.  
  46.     }         
  47.  
  48.              faverage=fsum/fcount;
  49.              caverage=csum/ccount;
  50.  
  51.                   cout<<fsum<<endl;
  52.                   cout<<csum<<endl;
  53.                   cout<<"\nNumber of times conversion to Fahrenheit requested: "<<fcount<<endl;
  54.                   cout<<"Number of times conversion to Celsius requested: "<<ccount<<endl;
  55.                   cout<<"Average of Fahrenheit outputs is "<<faverage<<endl;
  56.                   cout<<"Average of Celsius outputs is "<<caverage<<"\n\n";
  57.  
  58.     system("PAUSE");
  59.  
  60.     return 0;
  61. }
  62.  
  63. void ftoc()
  64. {          
  65.            float fahr,celsius,csum=0;
  66.            char temp;
  67.            int ccount=0;
  68.  
  69.                cout<<"\nEnter temperature in Fahrenheit: ";
  70.                cin>>fahr;
  71.                celsius = (fahr-32)*5/9;
  72.                cout<<"\nTemperature in Celsius is:";
  73.                cout<<celsius <<"\n";
  74. }
  75.  
  76. void ctof()
  77. {
  78.            float fahr,celsius,fsum=0;
  79.            char temp;
  80.            int fcount=0;
  81.  
  82.                cout<<"\nEnter temperature in Celsius: ";
  83.                cin>>celsius;
  84.                fahr = 32 + celsius*9/5;
  85.                cout<<"\nTemperature in Fahrenheit is:  ";
  86.                cout<<fahr <<"\n";
  87. }
  88.  
  89. void thrdoptn()
  90. {
  91.            cout<<"\nYour only choices are between Celsius and Fahrenheit. "
  92.            <<"\nChoose between F or C.\n";
  93. }
  94.  
Mar 3 '07 #1
3 2320
DeMan
1,806 1GB
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
Expand|Select|Wrap|Line Numbers
  1. float ctof() /* Declare a return type */
  2. {
  3.            float fahr,celsius,fsum=0;
  4.            char temp;
  5.            int fcount=0;
  6.  
  7.                cout<<"\nEnter temperature in Celsius: ";
  8.                cin>>celsius;
  9.                fahr = 32 + celsius*9/5;
  10.                cout<<"\nTemperature in Fahrenheit is:  ";
  11.                cout<<fahr <<"\n";
  12.         return fahr; /* Send this value back to the caller*/
  13. }
  14.  
You can then modify your main code to include something lioke

Expand|Select|Wrap|Line Numbers
  1. if (temp=='F' || temp=='f' || temp=='2')
  2. {
  3.   fsum = fsum + ctof();
  4.   fcount++;
  5. }     
  6.  
Mar 4 '07 #2
shk253
20
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
Expand|Select|Wrap|Line Numbers
  1. float ctof() /* Declare a return type */
  2. {
  3.            float fahr,celsius,fsum=0;
  4.            char temp;
  5.            int fcount=0;
  6.  
  7.                cout<<"\nEnter temperature in Celsius: ";
  8.                cin>>celsius;
  9.                fahr = 32 + celsius*9/5;
  10.                cout<<"\nTemperature in Fahrenheit is:  ";
  11.                cout<<fahr <<"\n";
  12.         return fahr; /* Send this value back to the caller*/
  13. }
  14.  
You can then modify your main code to include something lioke

Expand|Select|Wrap|Line Numbers
  1. if (temp=='F' || temp=='f' || temp=='2')
  2. {
  3.   fsum = fsum + ctof();
  4.   fcount++;
  5. }     
  6.  

Works like a charm!!! I agonized over this for quite a while and had given up. Thanks DeMan!!! You ROCK!
Mar 7 '07 #3
DeMan
1,806 1GB
Always a pleasure....
Mar 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
122
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) {...
4
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...
42
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 (...
10
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...
9
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...
7
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...
111
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;...
17
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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...
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.