Convert temperature between celsius and fahrenheit

I am supposed to write a program that allows the user to convert a temperature either from celsius to fahrenheit or fahrenheit to celsius and use separate functions for each conversion. I'm not sure if I'm headed in the right direction with my code, no matter what number I put in it always gives back the number 1.

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
#include <iostream>
using namespace std;

int CelsiusToFahrenheit(int nTempc, int nTempf) {
    int nResult;
    int i;
    
    nResult = 1;
    for (i = 1; i <= nTempf; i++) {
        nTempf = nTempc * (9 / 5) + 32;
    }
    
    return nResult;
    
}

int FahrenheittoCelsius (int nTempf, int nTempc) {
    int nResult;
    int i;
    
    for (i = 1; i <= nTempc; i++) {
        nTempc = (nTempf - 32) * (5 / 9);
    }
    
    return nResult;
}

int main() {
    int nInteger;
    int nTempc;
    int nTempf;
    int nResult;
    int i;
    
    cout << "Convert C to F (enter 1) or F to C (enter 2): ";
    cin >> nInteger;
    
    while (nInteger < 1 || nInteger > 2) {
        cout << "Invalid entry\n";
        cout << "Convert C to F (enter 1) or F to C (enter 2): ";
        cin >> nInteger;
    }
    
    nResult = CelsiusToFahrenheit(nTempc, nTempf);
    if (nInteger == 1) {
        cout << "Please enter a temperature in Celsius: ";
        cin >> nTempc;
    }
    if (nInteger == 1) {
        cout << nTempc << " Celsius equals " << nResult << " Fahrenheit";
    }
  
    
    
    nResult = FahrenheittoCelsius(nTempf, nTempc);
    if (nInteger == 2) {
        cout << "Please enter a temperature in Fahrenheit: ";
        cin >> nTempf;
        
    }
    if (nInteger == 2) {
        
        cout << nTempf << " Fahrenheit equals " << nResult << " Celsius";
        
    }
    
    
    
    return 0;
}
Last edited on
Please update edit your post and put your code between code tags to make it more readable. It's under the format section as <> - http://www.cplusplus.com/articles/jEywvCM9/
closed account (E0p9LyTq)
You have several issues with your code:

1. You are calling functions with uninitialized variables, before you get temperature values from the user.

2. You are getting round off errors courtesy of integer division.

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
#include <iostream>

float CelsiusToFahrenheit(float nTempc)
{
   return ((nTempc * (9.0 / 5.0)) + 32.0);
}


float FahrenheittoCelsius (float nTempf)
{
   return ((nTempf - 32.0) * (5.0 / 9.0));
}


int main()
{
   int nInteger;
   float nTempc;
   float nTempf;
   float nResult;

   std::cout << "Convert C to F (enter 1) or F to C (enter 2): ";
   std::cin >> nInteger;

   while (nInteger < 1 || nInteger > 2)
   {
      std::cout << "Invalid entry\n";
      std::cout << "Convert C to F (enter 1) or F to C (enter 2): ";
      std::cin >> nInteger;
   }

   if (nInteger == 1)
   {
      std::cout << "Please enter a temperature in Celsius: ";
      std::cin >> nTempc;
      nResult = CelsiusToFahrenheit(nTempc);
      std::cout << nTempc << " Celsius equals " << nResult << " Fahrenheit\n";
   }

   else
   {
      std::cout << "Please enter a temperature in Fahrenheit: ";
      std::cin >> nTempf;
      nResult = FahrenheittoCelsius(nTempf);
      std::cout << nTempf << " Fahrenheit equals " << nResult << " Celsius\n";
   }
}
Topic archived. No new replies allowed.