Calculator code issue

I have recently written code for a calculator. I've used functions to define how to do the operations. The user selects the operation using a character. That choose the if statement. My problem is no matter what the user choose the program works its way threw all the if statements.
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
77
78
79
80
81
82
83
#include <iostream>

using namespace std;

float division (float a, float b)
{
    float r1;
    r1=a/b;
    return (r1);
}
float multiplication (float c, float d)
{
    int r2;
    r2=c*d;
    return (r2);
}
float addition(float e, float f)
{
    int r3;
    r3=e+f;
    return(r3);
}
float subtraction(float g, float h)
{
    int r4;
    r4=g-h;
    return(r4);
}
int main()
{
    char opselect;
    float ma;
    float mb;
    float ans;

    cout<<"Welcome to Ryan's Calculator!\n\n";
    menu:
    cout<<"Please select an operation:\n\n";
    cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
    cout<<"Please use a letter to select the operation: ";
    cin>>opselect;

    if (opselect == 'D' || 'd')
    {
        cout<< "What two numbers would you like to divide: ";
        cin>>ma;
        cin>>mb;
        ans = division (ma, mb);
        cout<<"The answer is: "<<ans;
    }

    if (opselect == 'M' || 'm')
    {
        cout<< "What two numbers would you like to multiply: ";
        cin>>ma;
        cin>>mb;
        ans = multiplication (ma, mb);
        cout<<"The answer is: "<<ans;
    }

    if (opselect == 'A' || 'a')
    {
        cout<< "What two numbers would you like to add: ";
        cin>>ma;
        cin>>mb;
        ans = addition (ma, mb);
        cout<<"The answer is: "<<ans;
    }

    if (opselect == 'S' || 's')
    {
        cout<< "What two numbers would you like to subtract: ";
        cin>>ma;
        cin>>mb;
        ans = subtraction (ma, mb);
        cout<<"The answer is: "<<ans;
    }

    else
    {
        goto menu;
    }
}


All help will be appreciated!

Thanks,
Ryan
opselect == 'A' || 'a'


This doesn't do what you think it does, if you want to check if opselect == a or opselect == A do this:

if (opselect == 'a' || opselect == 'A')

Your code is checking to see if 'a' or 's' or 'm' and so on are true, which they really aren't, because they're characters.
To first help you out with the code



float division (float a, float b)
{
    float r1;
    r1=a/b;
    return (r1);
}
float multiplication (float c, float d)
{
    int r2;
    r2=c*d;
    return (r2);
}
float addition(float e, float f)
{
    int r3;
    r3=e+f;
    return(r3);
}
float subtraction(float g, float h)
{
    int r4;
    r4=g-h;
    return(r4);
}


If you look at your int r inside each function you see that you did float and then followed by int. Who is to say since this is a calculator that I might want to had 4.5 or 6.5 with 34.8?
all your int r should be float so that doesn't effect you. Secondly since I am sure that for you to use functions you probably had to deal understanding scopes. Each float/int r doesn't need to be called r1, r2.... since that variable is only being used by that function and that function only hence meaning that it is local you can call each one float r and r=a*b etc because these are local procedure in a function. plus it reduntant to accept demicals but not calculate them...

local ( in a specific function/class/structure that isn't made public ex. if, do_while,for, int name( int a, int b)

global can't be access from anywhere, for something to be declare global it has to be made outside of any function.

What happen to return 0; at the bottom?
goto menu;

It's considered really bad (and dangerous) practice to use goto. DON'T do it. There is always an alternative for this.
In your case use a loop like while and a condition to check.

All previous answers must be read also.
I made the suggested changes and everything is working great! Here's the new 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>

using namespace std;

float division (float a, float b)
{
    float r;
    r=a/b;
    return (r);
}
float multiplication (float a, float b)
{
    float r;
    r= a*b;
    return (r);
}
float addition(float a, float b)
{
    float r;
    r=a+b;
    return(r);
}
float subtraction(float a, float b)
{
    float r;
    r=a-b;
    return(r);
}
int main()
{
    char opselect;
    bool playagain = true;
    char playagainchar;
    float ma;
    float mb;
    float ans;

    cout<<"Welcome to Ryan's Calculator!\n\n";
    while (playagain == true)
    {
        cout<<"Please select an operation:\n\n";
        cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
        cout<<"Please use a letter to select the operation: ";
        cin>>opselect;

        if (opselect == 'D' || opselect == 'd')
        {
            cout<< "What two numbers would you like to divide: ";
            cin>>ma;
            cin>>mb;
            ans = division (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        if (opselect == 'M' || opselect == 'm')
        {
            cout<< "What two numbers would you like to multiply: ";
            cin>>ma;
            cin>>mb;
            ans = multiplication (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        if (opselect == 'A' || opselect == 'a')
        {
            cout<< "What two numbers would you like to add: ";
            cin>>ma;
            cin>>mb;
            ans = addition (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        if (opselect == 'S' || opselect == 's')
        {
            cout<< "What two numbers would you like to subtract: ";
            cin>>ma;
            cin>>mb;
            ans = subtraction (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }
    }


    cout<<"Thanks for calculating!";
    cin.get();
    return 0;
}
That is great I am happy for you :).
Topic archived. No new replies allowed.