Variables keep getting wrong values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shk253
    New Member
    • Feb 2007
    • 20

    #1

    Variables keep getting wrong values

    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";
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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
    Code:
    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

    Code:
    if (temp=='F' || temp=='f' || temp=='2')
    {
      fsum = fsum + ctof();
      fcount++;
    }

    Comment

    • shk253
      New Member
      • Feb 2007
      • 20

      #3
      Originally posted by DeMan
      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
      Code:
      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

      Code:
      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!

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Always a pleasure....

        Comment

        Working...