Functions will not do math

I can't get any output for celcius besides zero out of either function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
using namespace std;

void celcius(int);
void celciusloop();
int fatemp;



int main()

{
    int menu;
    
    cout<<"If you would like to convert a Farenheit temperature to celcius, press 1;" <<endl;
    cout<<"Otherwise, press 2 to see a demonstration of the converion program." <<endl;
    cin>> menu;
    
    while (menu >2|| menu<1)
         {
          cout<<"That entry is invalid, please re enter your selection: "; 
          cin>>menu; 
         }
   
    while (menu==1)
         {
          cout<<"This program will convert Farenheit to Celcius for you." <<endl;
    cout<<"Please enter the temperature in Farenheit:" <<endl;
    cin>>fatemp;
    cout<<"Thankyou";
    system ("pause");
          
          
          celcius(fatemp);
         cout<<"If you would like to enter another number to convert press 1, otherwise press 2 " <<endl;
         cout<<"for the demonstration, or any other number to exit." <<endl;
         cin>>menu;
         }
    
    if (menu==2)
       
                   {celciusloop();
                                 
                     system ("pause");
                     return 0;            
                                 
                   
                    
                    
                    }
   else
       {
       system ("Pause");
       return 0;   
       }
}  

    void celcius(int fatemp)
    
    { double celcius;
             celcius=5/9*(fatemp-32);
             cout<<setprecision(1) <<fixed;
             cout<<"The celcius conversion of your temperature is: " <<celcius <<"." <<endl;
             system ("pause");
             }
    
    
    
    void celciusloop()
    
    {
                  double celcius;
                  cout<<"Fahrenheit" <<setw(10) <<"Celcius" <<endl;
                  for (int count=0; count<21; count++)
                      {
                      celcius=5/9*(count-32);
                      cout<<setprecision(1) <<fixed;
                      cout<<count << "\t\t"<<left <<setw(5) <<celcius <<endl;
                      
                      }
    }




Dat formatting.

Anyways, try changing fatemp to a double as well.
You seem to be doing integer math in your calculations, 5/9 in integer math yields 0. There are no fractions with integers. You need to have at least one of your terms as a floating point type to force floating point math.
Change:
1
2
3
4
5
6
7
8
    void celcius(int fatemp)
    
    { double celcius;
             celcius=5/9*(fatemp-32);
             cout<<setprecision(1) <<fixed;
             cout<<"The celcius conversion of your temperature is: " <<celcius <<"." <<endl;
             system ("pause");
             }

to:
1
2
3
4
double FtoC(double f)
{
    return 5.0 / 9.0 * (f - 32.0);
}


Then change line 35 from: celcius(fatemp);
to: cout << "The celcius conversion of your temperature is: " << FtoC(fatemp) << endl;
Last edited on
Except you can't return an item from a void function. You also need to change the function to return a double.
We haven't learned what "ftoc" is in class, so my teacher wouldn't like if I used it. What is its purpose? Also, my funtions only have to display, they don't have to return anything to the main function.
That's dumb design. A function shouldn't be calculating and outputting. If he knows anything, he'll be more pleased if you go with Stewbond's route.
geharbison wrote:
We haven't learned what "ftoc" is in class,
You won't find "FtoC" in a textbook, it's just the name of a user-defined function, it can be anything you like. I expect Stewbond chose it as a concise and meaningful way of saying "Fahrenheit to Celsius".

so my teacher wouldn't like if I used it.
It's a function. I'm sure your teacher will be very pleased if you use functions.
Last edited on
Topic archived. No new replies allowed.