calculator problems

I just started to make my way in c++ and i'm trying to build a calculator.
everything is working fine except my "if" and "else" in case 4. it would be nice if anyone could help me and explain what i did wrong.

(i didn't include all my lines so it would be more clean)

1
2
3
4
5
6
7
8
9
10
11
12
switch(userChoice)
{  
case '4':
    cout << "enter number you want to divide " << X << " by" <<endl;
    cin >> Y;
    cout << " " << endl;
    if(Y = 0) {cout << "ERROR";
    }
    else
    {cout << "the result is " << (X / Y);
    }
    break;
if(Y = 0) What is the difference between = and ==?
thanks man i looked it up
I searched for many things but i didn't find a way for this. how can I make it work so i can start from the top if they choose Y after my last cout

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <math.h>

using namespace std;


int main()
{
    double X, Y;
    double resultPow;
    char userChoice;

    cout << " 1 - Add " << endl;
    cout << " 2 - Subtract " << endl;
    cout << " 3 - Multiply " << endl;
    cout << " 4 - Divide " << endl;
    cout << " 5 - Raise X to the power Y" << endl;
    cout << " 0 - Quit " << endl;
    cout << " " <<endl;   // voor een enter te hebben

    cout << "Enter first number" <<endl;
    cin >> X;
    cout << " " <<endl; // voor een enter the hebben

    cout << "enter choice" <<endl;
    cin >> userChoice;
    cout << " " << endl;

    switch(userChoice)   //userchoice is de veranderlijke
    {
    case '1':
        cout << "enter number you want to add to " << X <<endl;
        cin >> Y;
        cout << " " << endl;
        cout << "the result is " << (X + Y) <<endl;
        break;

    case '2':
        cout << "enter number you want to subtract from " << X <<endl;
        cin >> Y;
        cout << " " << endl;
        cout << "the result is " << (X - Y) <<endl;
        break;

    case '3':
        cout << "enter number you want to multiply " << X << "with" <<endl;
        cin >> Y;
        cout << " " << endl;
        cout << "the result is " << (X * Y);
        break;

    case '4':
        cout << "enter number you want to divide " << X << " by" <<endl;
        cin >> Y;
        cout << " " << endl;
        if(Y == 0) {cout << "ERROR";
        }
        else
        {cout << "the result is " << (X / Y);
        }
        break;

    case '5':
        cout << "enter number you want to raise " << X << " to" << endl;
        cin >> Y;
        resultPow = pow (X,Y);
        cout << "\n Answer = " << resultPow <<endl;
        break; }

    cout << "do you want to calculate something else? Y/N " <<endl;
       

    }

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void calc()
{
    //Here is all actual calculation code which was previously in main()
}

int main()
{
    char choice;
    do {
        calc();
        std::cout << "do you want to calculate something else? Y/N\n";
        std::cin >> choice;
        //Additional input and lowercase  handling optional
    } while(choice == 'Y');
}
wow thanks for the fast reply and help!
Topic archived. No new replies allowed.