-3

I am writing a Fahrenheit to Celsius converter that also converts Celsius to Fahrenheit using a switch break feature in C++. The program is where the user enters in a number followed by a "c" for Celsius or an "f" for Fahrenheit, then they hit enter and the program will calculate the conversion and display the temperature converted. The Fahrenheit to Celsius conversion works and is accurate, however the Celsius to Fahrenheit calculation always converts to 1.66607e+62 no matter what number of fahrenheit you enter. Also it doesnt calculate on the first time you enter in the temperature, you have to enter it in twice before it works and converts it. I am just wondering what do I have to do to fix these problems thanks. Heres my code :

#include <iostream> //cout
#include <conio.h> //getch

using namespace std;

int main()    
{        
    double celsius, fahrenheit, temp;

    char unit;

    cout << "Enter the temperature you wish to convert followed by F for Fahrenheit or C for Celsius: " << endl;
    cin >> temp;
    cin >> unit;
    cin >> fahrenheit;
    cin >> celsius;    

    switch (unit)
    {
    case 'F':         temp = fahrenheit;
        celsius = (5.0 / 9.0) * (fahrenheit - 32.0);   
        cout << celsius << " degrees celsius";                                          
        break;

    case 'f':         temp = fahrenheit;
        celsius = (5.0 / 9.0) * (fahrenheit - 32.0);      
        cout << celsius << " degrees celsius";                                          
        break;

    case 'C':         temp = celsius;
        fahrenheit = (9.0 / 5.0) * celsius + 32.0;      
        cout << fahrenheit << " degrees fahrenheit"; 
        break;

    case 'c':         temp = celsius;
        fahrenheit = (9.0 / 5.0) * celsius + 32.0; 
        cout << fahrenheit << " degrees fahrenheit";                                      
        break;      

    default:        cout << "Invalid Format";               
        break;    
    }

    _getch();

    return 0;
}
DYZ
  • 55,249
  • 10
  • 64
  • 93
E.Mules
  • 19
  • 2
  • 8
  • 4
    Partly related to your problem, but you should probably [get a couple of good beginners books](https://stackoverflow.com/a/388282/440558) to read, because there are a few mistakes you make. Like why you ask the user to input *four* values when you only need *two* (this is related to one of your problems)? And that `case` fall through meaning you don't need the duplicate code. – Some programmer dude Feb 12 '18 at 00:00
  • When you test your program, do you _always_ provide four inputs (a number, a character, and two more numbers), as your program expects? – DYZ Feb 12 '18 at 00:02
  • You need to provide the exact input you are providing and what errors you are seeing. – xaxxon Feb 12 '18 at 00:06
  • If you converted your input to all lower case or all upper case, you would require half as many comparisons or cases. See `toupper` and `tolower`. – Thomas Matthews Feb 12 '18 at 00:16

2 Answers2

0

You have 4 inputs, you probably just enter two first and then the program still waits for your response. Then you just "enter" those two variables with blank value and use them in calculations. You also reasign "temp" variable and you just loose this. Like someone said, you should read some books for beginners.

0

fixed the issue for you!

#include <iostream> //cout
#include <conio.h> //getch

using namespace std;

int main()
{
   double celsius, fahrenheit, temp;

char unit;

cout << "Enter the temperature you wish to convert followed by F for Fahrenheit or C for Celsius: " << endl;
cin >> temp;
cin >> unit;
switch (unit){
case 'F':
    celsius = (5.0 / 9.0) * (temp - 32.0);
    cout << celsius << " degrees celsius";
    break;

case 'f':
    celsius = (5.0 / 9.0) * (temp - 32.0);
    cout << celsius << " degrees celsius";
    break;

case 'C':
    fahrenheit = (9.0 / 5.0) * temp + 32.0;
    cout << fahrenheit << " degrees fahrenheit";
    break;

case 'c':
    fahrenheit = (9.0 / 5.0) * temp + 32.0;
    cout << fahrenheit << " degrees fahrenheit";
    break;

default:
    cout << "Invalid Format";
    break;
}

_getch();

return 0;
}

EDIT: Switch case statements have a "fall through" behaviour. Thus, you can put the cases for 'F' and 'f' in the together (and similarly 'C' and 'c') as suggested by a comment on this post, by doing the following:

#include <iostream> //cout
#include <conio.h> //getch

using namespace std;

int main()
{
   double celsius, fahrenheit, temp;

char unit;

cout << "Enter the temperature you wish to convert followed by F for Fahrenheit or C for Celsius: " << endl;
cin >> temp;
cin >> unit;
switch (unit){
case 'F':
case 'f':
    celsius = (5.0 / 9.0) * (temp - 32.0);
    cout << celsius << " degrees celsius";
    break;

case 'C':
case 'c':
    fahrenheit = (9.0 / 5.0) * temp + 32.0;
    cout << fahrenheit << " degrees fahrenheit";
    break;

default:
    cout << "Invalid Format";
    break;
}

_getch();

return 0;
}

If this is a school assignment, this is probably what they want you to do (and why you were supposed to use the switch statement.)

Umer Amjad
  • 68
  • 6
itsrajon
  • 283
  • 2
  • 15
  • 1
    While you're fixing people's code, you could make the `F` and `f` case share the same code, similarly with the other case. Or you could tell the OP *how* to fix the code so that they can learn. – Thomas Matthews Feb 12 '18 at 00:18
  • you are right! i will be careful from now on. Thank you :) – itsrajon Feb 12 '18 at 00:23
  • Thank you so much @Pusku!! this helped me out tremendously!! I apologize for bad question asked, I am just trying to learn this language and I have been stuck on this for some time now. – E.Mules Feb 12 '18 at 00:25
  • You can also suggest the OP to not use the non-standard header ``. – Xam Feb 12 '18 at 01:41