temperature conversion assistance

New Member


Join Date: Oct 11
Posts: 1
C++ temperature conversion assistance

--------------------------------------------------------------------------------

Hi everyone I am trying to make a program that has the user enter the number followes by a space and the letter f or c for fahrenheit or celcius. i need the program to convert it to the opposite degrees. so if i enter 58 c i need it to convert to fahrenheit, for some reason my program is not working right. It compiles but the numbers are way off. i need to use the formulas that i have in place. Can anyone tell me what I am missing?
Thanks:



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

using namespace std;  

   int main()  

{  
   char change;  

    float num1;  

    cout << " enter temp and one space and then f or c for Celsius or Fahrenheit :" << endl;  

   cin >> choice>> num1;  

   if (change == 'c')  

    {  
   float cs1 =  5.0 / 9.0 * (num1 - 32);
   cout << num1 << " degrees Celsius is " << cs1 << " degrees Fahrenheit" << endl;  
 }  
 else if
 {  

 float fah= (5.0/9.0)* (num1+ 32);  
 cout << num1 << "degrees Fahrenheit is " << fah << "degrees Celsius" << endl;  
}  

{  
 else
 cout<<"You have entered incorrect data"<<endl;

    return 1;  
}
} 
Last edited on
It compiles


It actually doesn't. You have numerous errors.

- your variable is named 'change' but you refer to it as 'choice'
- F to C code block is missing an if statement
- your { brace on the final else is misplaced

After correcting those errors I got it to compile. And noticed 2 problems:

1) In the first if block you are using the F to C formula when you should be using the C to F formula
2) In the second block, that formula is all wrong. It should be F to C. Double check your formulas.
3) You are taking input backwards from what the assignment dictates. You are getting 'choice' first then 'num1', which means proper input would look like "f 65" instead of "65 f"
Hi sorry for all those silly errors. I corrected most of them but I don't see where I forgot an if statement between the F to C code. Also i nthe second block do you know why the formula is wrong? I switched the formulas for they are the right ones. I think I fixed all the errors. I posted below any feedback is appreciated. Thank You for the tips.
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
#include <iostream>  

using namespace std;  

   

int main()  

{  

    char choice;  

    float num1;  

   

    cout << " enter temp and one space and then f or c for Celsius or Fahrenheit :" << endl;  

   cin >> num1>> choice;  

   

    if (choice== 'c')  

    {  

         

        float cs1 =  (5.0/9.0) * (num1+ 32);
  

        cout << num1 << " degrees Celsius is " << cs1 << " degrees Fahrenheit" << endl;  

    }  

     if (choice=='f')

    {  

        

        float fah= 5.0 / 9.0 * (num1 - 32);  

        cout << num1 << "degrees Fahrenheit is " << fah << "degrees Celsius" << endl;  

    }  

     else
{
 cout<<"You have entered incorrect data"<<endl;

    return 1;  
}
} 
Last edited on
Your C to F formula is still wrong.

do you know why the formula is wrong?


Yes. Don't you? Do you know the C->F and F->C formulas?

Look at the formula, then look at what your code is doing.
Last edited on
Topic archived. No new replies allowed.