Celcius and Fahrenheit (Need Assistance)

Celsius and Fahrenheit

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.




Any and all help would be appreciated as this is all I could come up with because finals are coming p and I need to get this and many other assignments done asap


#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
cout << "Enter c (or C) to convert Fahrenheit to Celsius" << endl;cout << " or f (or F) to convert Celsius to Fahrenheit: ";
char userinput;
cin >> userinput;
if ( userinput == 'c' || userinput == 'C' ){cout << endl;
cout << "Enter the temperature: ";double temperatureCone;
double temperatureFone;
cin >> temperatureCone;
temperatureFone = ( 9.0 / 5.0 ) * temperatureCone + 32.0;
cout << fixed << setprecision(1);
cout << temperatureCone << " Celsius is " << temperatureFone << " degrees Fahrenheit." << endl;
}
if ( userinput == 'f' || userinput == 'F' ){cout << endl;
cout << "Enter the temperature: ";
double temperatureFtwo;
double temperatureCtwo;
cin >> temperatureFtwo;
temperatureCtwo = ( temperatureFtwo - 32 ) * ( 5 / 9 );
cout << fixed << setprecision(1);
cout << temperatureFtwo << " Fahrenheit is " << temperatureCtwo << "degrees Celsius." << endl;
}
if ( userinput == 'a' || userinput == 'b' || userinput == 'd' || userinput== 'e' || userinput == 'g' || userinput == 'h' || userinput == 'i' || userinput
Last edited on
That looks like you've accidentally only pasted a part of your code.
That's part of what I need help with actually. I only got this far from having a tutor show me what to do.
Please use code tags. Edit your post, highlight the code and press the <> button to the right of the edit window. This will make your code appear with line numbers so we can refer to it.

Here is the code you have:
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
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
    cout << "Enter c (or C) to convert Fahrenheit to Celsius" << endl;cout << "\
 or f (or F) to convert Celsius to Fahrenheit: ";
    char userinput;
    cin >> userinput;
    if ( userinput == 'c' || userinput == 'C' ){cout << endl;
        cout << "Enter the temperature: ";double temperatureCone;
        double temperatureFone;
        cin >> temperatureCone;
        temperatureFone = ( 9.0 / 5.0 ) * temperatureCone + 32.0;
        cout << fixed << setprecision(1);
        cout << temperatureCone << " Celsius is " << temperatureFone << " degre\
es Fahrenheit." << endl;
    }
    if ( userinput == 'f' || userinput == 'F' ){cout << endl;
        cout << "Enter the temperature: ";
        double temperatureFtwo;
        double temperatureCtwo;
        cin >> temperatureFtwo;
        temperatureCtwo = ( temperatureFtwo - 32 ) * ( 5 / 9 );
        cout << fixed << setprecision(1);
        cout << temperatureFtwo << " Fahrenheit is " << temperatureCtwo << "deg\
rees Celsius." << endl;
    }
    if ( userinput == 'a' || userinput == 'b' || userinput == 'd' || userinput=\
= 'e' || userinput == 'g' || userinput == 'h' || userinput == 'i' || userinput

Line 19: add else and combine with line 20
Line 25: Change 5 / 9 to 5.0 / 9.0. 5/9 invokes integer division and creates an integer result of 1.
Change lines 30 & 31 to
1
2
3
else {
        cout << "Invalid input\n";
    }

That will get the code to compile. Run some simple tests and you'll find that you still have some problems but I think you'll be able to figure them out.

Topic archived. No new replies allowed.