Temperature Conversion Program

Hi guys I'm new to C++ and I'm having a small issue with a homework assignment. The following are my professor's instructions. Start by writing a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is F = (9/5)C + 32 where F is the Fahrenheit temperature and C is the Celsius temperature. Enhance the program by allowing the user to choose between converting from Celsius to Fahrenheit and vice versa. See the below output. You will need to look up the formula for the other directions (simply google it!). Allow for both upper and lower-case menu item selections and generate an appropriate error message if the user enters something unexpected. Format your output to always show one decimal. You will build on this assignment in future chapters.

The output should look something like this:
Enter c (or C) to convert Fahrenheit to Celsius
or f (or F) to convert Celsius to Fahrenheit: c

Enter the temperature: 0.0
0.0 Celsius is 32.0 degrees Fahrenheit.

Running the program again:
Enter c (or C) to convert Fahrenheit to Celsius
or f (or F) to convert Celsius to Fahrenheit: F

Enter the temperature: 212
212.0 Fahrenheit is 100.0 degrees Celsius.

Here's the code I currently have, converting fahrenheit to celsius is working fine but I cant get celsius to farenheit to work. Any guidance would be appreciated.

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

int main() {
    float choice;
    float celsius;
    float fahrenheit;

    char input1;
    double input2;



    cout << "Enter c (or C) to convert Fahrenheit to Celsius or f (or F) to convert Celsius to Fahrenheit: \n";
    cin >> input1;


    if (input1 == 'c')
    {
      cout << "Enter your temperature in Fahrenheit: ";
      cin >> input2;
      cout << endl;
      cout << "Your temperature in Celsius is: " << (input2 - 32) * (5.0/9.0) << endl;
    }

    else if (input1 == 'C')
    {
      cout << "Enter your temperature in Fahrenheit: ";
      cin >> input2;
      cout << endl;
      cout << "Your temperature in Celsius is: " << (input2 - 32) * (5.0/9.0) << endl;
    }

    if (input1 == 'f')
  {
    cout << "Enter your temperature in Celsius: ";
    cin >> celsius;
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Your temperature in Fahrenheit is: " << fahrenheit << endl;
    //cin >> input2;
    //cout << endl;
    //cout << "Your temperature in Fahrenheit is: " << (input2 * 9.0/5.0) + (32) << endl;
  }

  else if (input1 == 'F')
  {
    cout << "Enter your temperature in Celsius: ";
    cin >> input2;
    cout << endl;
    cout << "Your temperature in Fahrenheit is: " << (input2 * 1.8) + (32) << endl;
  }

    return 0;
}
> I cant get celsius to farenheit to work
¿what do you mean?

apart
input1, input2 are terrible names
you've got duplicated code when you may simply change the condition
1
2
3
char choice;
if (choice == 'c' or choice == 'C')
if (tolower(choice) == 'c')


¿who wrote lines 36--39?
When running the program, the conversion from celsius to farenheit works but not the conversion from farenheit to celsius.

Lines 36-39 were how I saw an online tutorial on youtube for celsius to farenheit work.

Like I said I'm new to coding in general, so I'm not sure exactly what to name things, this is an online course and it is difficult to follow.
Hello ajohns2,

When it comes to naming a variable choosing a name that describes what it is or what it does is better than just coming up with something.

In your code you define "float choice;", but never use it. The variable "input1" would work better as just "input", but look at what you are doing. You are asking the user to make a choice between "c" or "f". Using "choice" is a better choice for the variable name.

You have defined "celsius", no offense, but it is not spelled correctly, and "fahrenheit" as "floats". This may work, but could be a problem later. These days "double" is the preferred floating point type as it has greater precision than a 'float". Also when you put something like "5.0" or "9.0" in your formula these numbers are considered "doubles" by the IDE and compiler. Putting a "double", with up to 20 decimal places, into a "float" with 15 decimal places could cause a data loss when it drops the extra decimal places.

When writing the if/else if statements consider this. Your are asking for either "c" or "f" from the user, so what you could do is:
1
2
3
4
if(std::tolower(choice) == 'c")
    // code for choice "c"
else
    // Code for choice "f" 

Since you only have two choices a simple if/else will work. This will also cout out the redudant code that you have.

Instead of using "input2" this is where you can use "celsius" and "fahrenheit" and it makes the code easier to read and understand.

Give it some thought and see what you come up with.

Hope that helps,

Andy
> converting fahrenheit to celsius is working fine but I cant get celsius to farenheit to work
> the conversion from celsius to farenheit works but not the conversion from farenheit to celsius. \
... ¿which one?
your formula appears correct, and passed some of my testcases
¿for what input are you saying that it fails? ¿and what's the failure? (wrong value, crash, freeze)
@ness555

for an input of either "f" or "F" results in a wrong value for some reason
Hello ajohns2,

The formula on lines 23 and 31 are correct

The formula on line 42 is correct, but you do not need the () around the 32.

The formula on line 50 is incorrect. It is possible that you are trying to use a variation here, but have it wrong.

I did a Google search and came up with this that helped. https://www.google.com/search?source=hp&ei=PuhjXIuYOqXCjwTiiZqgCQ&q=formula+to+convert+celcius+to+farenheit&oq=formula+to+convert+c&gs_l=psy-ab.1.1.0l10.1280.8241..11687...0.0..0.188.2013.17j4......0....1..gws-wiz.....0..0i131.XJo48FXEZyc

Hope that helps,

Andy
> The formula on line 50 is incorrect.
no, it isn't


> for an input of either "f" or "F" results in a wrong value for some reason
$ ./a.out
Enter c (or C) to convert Fahrenheit to Celsius or f (or F) to convert Celsius to Fahrenheit: 
F
Enter your temperature in Celsius: 
0
Your temperature in Fahrenheit is: 32
$ ./a.out
Enter c (or C) to convert Fahrenheit to Celsius or f (or F) to convert Celsius to Fahrenheit: 
f
Enter your temperature in Celsius: 
100
Your temperature in Fahrenheit is: 212
that's what I obtain when running your program
¿do you have another result? in that case post your input and the output that produces (I've already asked you this)
Topic archived. No new replies allowed.