Program closes

Hi,

I'd like to ask for your help. I am a begginer in C++ and I just wrote a calculator, but as soon as I choose an option, the console immediately closes.

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

using namespace std;

int main()
{
    int answer;
    float y,z;

    cout << "First number: ";
    cin >> y;
    cout << "Second number: ";
    cin >> z;

    cout << "\n1. Addition\n";
    cout << "2. Substraction\n";
    cout << "3. Multiplying\n";
    cout << "4. Division\n";
    cout << "5. Bring to square\n";
    cout << "6. 1/x\n";
    cout << "7. % \n";
    cout << "8. Square root\n";
    cin >> answer;

    switch (answer) {
    case 1:
        cout << "Result: " << y+z;
        break;
    case 2:
        cout << "Result: " << y-z;
        break;
    case 3:
        cout << "Result: " << y*z;
        break;
    case 4:
        cout << "Result: " << y/z;
        break;
    case 5:
        cout << "Result: " << y*y;
        break;
    case 6:
        cout << "Result: " << 1/y;
        break;
    case 7:
        cout << "Result: " << y*(z/100);
        break;
    case 8:
        cout << "Result: " << sqrt(y);
        break;
    default:
        cout << "Bad input.";
        break;
    }
    cin.get();
    return 0;
}


Can anybody help me? I have experienced this problem in other programs as well so most certainly I cause the problem by an error.
Last edited on
Crashes, or closes?

If using Windows, I suspect is that it closes; it doesn't crash.
Sorry, my bad. Yes, it closes.
hello.y read and run you're code..it is perfect. no errors and it works just fine,maybe the problem is somewhere else.try to put a #include <stdlib.h> and just before the return 0; put system("pause"); this will stop the program when it finishes and let's u see the console with the final result .then you press any key and then it closes.
Thank you all for help, Mihay07's suggestion perfectly works. :)
Last edited on
duoas,y read about the danger of using it,and y did not really understand all but y don't say you're wrong..but for us ppl,that try to make small programs,exercises from a book,or just trying to learn c++ y will say it is ok..but if you write serious programs,and stuff like that,for different people,it maybe a serious problem.
An alternative to system("pause"); ,that y use is to simply write getchar(); in the place of it.and you can put a nice message before it so it loks fineprintf("Press Enter to continue");.this will wait for you to press enter and then the console will exit.and you don't need the stdlib library any more..is that wrong to?
Last edited on
No, even stupid programs should not have dangerous code in it -- right from the start. If you learn to do things the right way to begin with, you will never have to struggle to rid yourself of bad habits, and you'll never be fired or prosecuted for them.

User input code must be robust. Using getchar(); works, but for the wrong reasons. Better to use cin.ignore( numeric_limits <streamsize> ::max(), '\n' );.

All but the most trivial C++ programs are linked to the stdlib, whether or not you explicitly use it.

Hope this helps.
ok.. y now that bad habits are not good..to be honest y have no clue what that piece of code is,or what it does.maybe because y don't have the knowledge you have...but if you say it is the best way,y will use it..tnx
Topic archived. No new replies allowed.