I wrote a calculator code, want some advice to improve.

Write your question here.
This is my own calculator code, and I want to improve it. Please give me some advice.
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
  #include <iostream>

using namespace std;

int main()
{
    string name;
    cout << "What's your name?\n";
    getline (cin, name);
    cout << "Hello, " << name << "!\n";

    float a;
    cout << "Enter a number \n";
    cin >> a;

    float b;
    cout << "Enter another one \n";
    cin >> b;

    int x;
    cout << "Choose a desired function: 1 for add, 2 for sub, 3 for multiply, 4 for divide \n";
    cin >> x;

    if (x==1){
        cout << "a+b=" << a+b << endl;
    }
    if (x==2){
        cout << "a-b=" << a-b << endl;
    }
    if (x==3){
        cout << "a*b=" << a*b << endl;
    }
    if (x==4){
        cout << "a/b=" << a/b << endl;
    }

    return 0;
}
Last edited on
Hi,

Integer division won't work, make the types double.

Everyone seems to forget to check for division by zero. With doubles this is a little trickier:

1
2
3
4
5
6
7
#include <cmath>

const double precision = 1e-3; // 0.001 set this to whatever you want

if (std::abs(b) < precision) {
     std::cout "Division by zero error\n"
}


Place an else on line 36 to catch bad input. Or use a switch statement with a default: case, instead of the if else if chain.

Good Luck !!
thanks!
here's my new code but it doesn't run properly, where should I modify?

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

using namespace std;

int main()
{
    string name;
    cout << "What's your name?\n";
    getline (cin, name);
    cout << "Hello, " << name << "!\n";

    float a;
    cout << "Enter a number \n";
    cin >> a;

    float b;
    cout << "Enter another one \n";
    cin >> b;

    int x;
    cout << "Choose a desired function: 1 for add, 2 for sub, 3 for multiply, 4 for divide \n";
    cin >> x;

    char function = 'x';

    switch(function)
    {
    case '1' :
        cout << "a+b=" << a+b << endl;
        break;
    case '2' :
        cout << "a-b=" << a-b << endl;
        break;
    case '3' :
        cout << "a*b=" << a*b << endl;
        break;
    case '4' :
        cout << "a/b=" << a/b << endl;
        break;
    default :
        cout << "Invalid input.\n";
    }
    cout << "result:" << function << endl;


    return 0;
}
Last edited on
You were checking function rather than your integer variable x.
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
#include <iostream>

using namespace std;

int main()
{
    string name;
    cout << "What's your name?\n";
    getline (cin, name);
    cout << "Hello, " << name << "!\n";

    float a;
    cout << "Enter a number \n";
    cin >> a;

    float b;
    cout << "Enter another one \n";
    cin >> b;

    int x;
    cout << "Choose a desired function: 1 for add, 2 for sub, 3 for multiply, 4 for divide \n";
    cin >> x;

    char function = 'x';

    switch(x) //Changed function to x, as it will always be 'x' and nothing else.
    { //Also changed the cases to integers.
    case 1 :
        cout << "a+b=" << a+b << endl;
        break;
    case 2 :
        cout << "a-b=" << a-b << endl;
        break;
    case 3 :
        cout << "a*b=" << a*b << endl;
        break;
    case 4 :
        cout << "a/b=" << a/b << endl;
        break;
    default :
        cout << "Invalid input.\n";
    }
    //Got rid of result as your case already delivers that to you.
    return 0;
}
Thanks!
Topic archived. No new replies allowed.