cant get switch statement to work

Jun 15, 2011 at 2:27am
hi, ive been trying to work on a celcius to ferenheit calculator, and ferenheit to celcius calculator. I tried to make it so that you could enter 'f' so that you can convert ferenheit to celcius, and 'c' to convert celcius to ferenheit. For some reason, when i run the program and enter one of the options, nothing happens and the programs just ends. here's the code:

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

using namespace std;

int main()
{
    int ferenheit;
    int celcius;
    int choose;

    cout << "Enter 'c' to convert celsius to ferenheit, or 'f' for ferenheit to celsius" << endl;
    cin >> choose;

    switch (choose){
    case 'f':
    cout << "please enter the ferenheit." << endl;
    cin >> ferenheit;
    celcius = 0.555 * (ferenheit - 32);
    cout << "the celcius is: " << celcius << endl;
    break;
    case 'c':
    cout << "please enter the celsius" << endl;
    cin >> celcius;
    ferenheit = 1.8 * (celcius + 32);
    cout << "the ferenheit is: " << ferenheit << endl;
   break;
    }

    return 0;
}
Last edited on Jun 15, 2011 at 2:27am
Jun 15, 2011 at 2:29am
You have to use different data types. int can only store integer numbers, while double can store real numbers and char can store characters.
Jun 15, 2011 at 2:31am
wow, just noticed. sorry that was a dumb mistake by me. Thanks though
Jun 15, 2011 at 2:35am
It could still work as is if the user enters 99 (ascii 'c') or 102 (ascii 'f').
Topic archived. No new replies allowed.