Program Closes After Inputting Value

I'm writing a program for which I'm having a lil' trouble. While the code doesn't seem to bring up any errors, the resulting program closes after I input a value.

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
56
57
58
59
60
61
62
#include <iostream.h>
int main ()
{
          double temp;
          double c;
          double f;
          double k;
          int value, quit;

    cout << "Temperate Conversion Calculator";
    cout << "\n\nPlease enter the temprature unit for which you want to covernt.";

    cout <<  "\n1. F for Fahrenheit to Celcius and Kalvin";
    cout <<  "\n2. C for Celsius to Fahrenheit and Kalvin";
    cout <<  "\n3. K for Kalvin to Fahrenheit and Celcius";
    beginning:
    cout <<  "\n\nPlease enter your option:";
         cin >> value; 
                 
    switch (value) { 
    case 'F':
    case 'f': 
        cout << "Please enter temprature in Farhenheit: ";
        cin >> temp;
        c = ((temp - 32) * 5/9);
        k = ((f + 459.67) * 5/9);
        cout << "Celsius:"<< c;
        cout << "\n Kelvin:"<< k;
        cout << "\n Do you want to calculate another value (y/n): ";
        cin >> quit;
        if (quit == 'y' || quit == 'Y')
        goto beginning;        
        break;
    case 'C':
    case 'c':
        cout << " Please enter temprature in Celsius: ";
        cin >> temp;
        f = (c* 5/9 + 32);
        k = c + 273.15; 
        cout << " Fahrenheit:"<< f;
        cout << "\n Kelvin:"<< k;
        cout << "\n Do you want to calculate another value (y/n): ";
        cin >> quit;
        if (quit == 'y' || quit == 'Y')
        goto beginning; 
        break;
    case 'K':
    case 'k':
        cout << " Please enter temprature in Kelvin: ";
        cin >> temp;
        f = ((k - 273) * 9/5);
        c = k - 273.15;
        cout << " Fahrenheit:"<< f;
        cout << "\n Celsius:"<< c;
        cout << "\n Do you want to calculate another value (y/n): ";
        cin >> quit;
        if (quit == 'y' || quit == 'Y')
        goto beginning; 
        break;
         
     }
}


This is my first time at C++ so there's still a lot I'm not familiar with.

EDIT: Oh, I suppose I should mention I get a error message for iostream.h. I change it to iostream but then when I try to compile and run it, it gives me a cout and cin undeclared error.
Last edited on
What happens when you execute the break statement in any of the switch branches? You go to the statement after the switch statement, which is implicitly the end of the program and it of course exits.

Your cin and if statements belong after the switch statement. No point in repeating the same logic in every case. This will also fix the problem of your program exiting unexpectedly.
After line 61:
1
2
3
4
5
  cout << "\n Do you want to calculate another value (y/n): ";
  cin >> quit;
  if (quit == 'y' || quit == 'Y')
        goto beginning;        
  return 0;  // exit the program 


BTW, the use of goto is poor style. A loop is better suited and should be obvious once you move the above lines.

edit: Your error with cin and cout undefined is because you need: using namespace std; cin and cout are in the std namespace.
Last edited on
Thanks for your response.

So I did just that however I'm still receiving the same results. :(

I can see now that it indeed is jumping right to the end as before the program closes I was able to catch the last 'Do you want to calculate...' line appear.
You should insert a loop to keep your program opened until some conditions are met to close it.
Sorry if this is a dumb question as I find myself rather lost, how exactly would I write in the loop?
Perhaps like this:
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
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main ()
{
    char value;

    cout <<  "\n  F for Fahrenheit to Celsius and Kelvin";
    cout <<  "\n  C for Celsius to Fahrenheit and Kelvin";
    cout <<  "\n  K for Kelvin to Fahrenheit and Celsius";
    cout <<  "\n  Q to quit";
    
    do 
    {
        cout <<  "\n\n  Please enter your option: ";
        cin >> value; 

        switch (value) 
        { 
            case 'F':
            case 'f':      
                break;
                
            case 'C':
            case 'c':
                break;
                
            case 'K':
            case 'k':
                break;

            case 'q':
            case 'Q':
                break;

            default:
                cout << "Invalid option" << endl;
        }
    } while (value != 'q' && value != 'Q');
    
}
Last edited on
That did it. :) Thank you so much Chervil! Abstraction and minuss as well!
Topic archived. No new replies allowed.