Why is the answer to every operation wrong?

This is my calculator project I've been working on. But, every time i try to get an answer, it's always wrong.

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
104
105
106
107
108
109
110
111
112
113
#import <iostream>

using namespace std;

double addition (double a, double b)
{
    double z;
    z = a + b;
    return (z);
}
double subtraction (double a, double b)
{
    double z;
    z = a - b;
    return (z);
}
double multiplication (double a, double b)
{
    double z;
    z = a * b;
    return (z);
}
double division (double a, double b)
{
    double z;
    z = a / b;
    return (z);
}
int main ()
{
    long double aa;
    long double ab;
    long double ac;
    char add;
    char sub;
    char mlp;
    char div;
    char a;
    char b;
    char c;
    char res;
    c = '\a';
    b = '\0';
    a = '\0';
    res = 'r';
    add = 'a';
    sub = 's';
    mlp = 'm';
    div = 'd';
    int_main:
    cout << "Please choose a mathematical method. (a/s/m/d): ";
    cout << endl;
    cin >> a;
    if (a == add)
    {
    ac = addition (aa,ab);
    cout << "You have chosen addition. Please provide a numerical value: ";
    cout << endl;
    cin >> aa;
    cout << "Please provide another numerical value: ";
    cout << endl;
    cin >> ab;
    cout << "Your result value is " << ac << "." << endl;
    goto int_main;
    }
    else if (a == sub)
    {
    ac = subtraction (aa,ab);
    cout << "You have chosen subtraction. Please provide a numerical value: ";
    cout << endl;
    cin >> aa;
    cout << "Please provide another numerical value: ";
    cout << endl;
    cin >> ab;
    cout << "Your result value is " << ac << "." << endl;
    goto int_main;
    }
    else if (a == mlp)
    {
    ac = multiplication (aa,ab);
    cout << "You have chosen multiplication. Please provide a numerical value:";
    cout << endl;
    cin >> aa;
    cout << "Please provide another numerical value: ";
    cout << endl;
    cin >> ab;
    cout << "Your result value is " << ac << "." << endl;
    goto int_main; 
    }    
    else if (a == div)
    {
    ac = division (aa,ab);
    cout << "You have chosen division. Please provide a numerical value: ";
    cout << endl;
    cin >> aa;
    cout << "Please provide another numerical value: ";
    cout << endl;
    cin >> ab;
    cout << "Your result value is " << ac << "." << endl;
    goto int_main;
    }
    else
    {
    else_final:
    cout << "I'm sorry, but " << a << " is not a valid mathematical operation." << endl;
    cout << "Press/enter 'r' to restart." << endl;
    cout << "Press/enter any other character to quit." << endl;
    cin >> b;
    if (b == res)
    goto int_main;
}
    return 0;
}


Can anyone debug this and repost? Telling me what i did wrong would be good, too.
But, every time i try to get an answer, it's always wrong.


A sample output compared to expected output would be helpful.

1
2
3
4
5
6
7
8
ac = addition (aa,ab);
    cout << "You have chosen addition. Please provide a numerical value: ";
    cout << endl;
    cin >> aa;
    cout << "Please provide another numerical value: ";
    cout << endl;
    cin >> ab;
    cout << "Your result value is " << ac << "." << endl;


This is backwards, you call the function before you set values to aa and ab. Same thing for the rest of them, you always call the function before you assign values.

One thing that tripped me up a little bit was the variables, why do you have so many? a == add could be replaced with a comparison to a specific character (if (/*char*/a == 'a') { //code and such} ).

Can anyone debug this and repost?


It can be done, it's better if you apply the suggested fixes yousel...

Telling me what i did wrong would be good, too.


I laughed so hard. Were you expecting someone to fix it and give it to you?
For starters You are calling all of your operation functions before you get their needed inputs. So the outputs you are getting will be whatever aa and ab are initialized to, which since you didn't specify it will be some random number...

also I might recommend using an enum type for your operation selection and simply a 1-4 selection menu.

That part would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int choice=1;
enum Operation { ADD=1,SUB,MLP,DIV};

cout <<" Select Operation:\n"
        <<"1. Add\n"
        <<"2.Subtract\n"
        // you can complete it from here
cin >> choice;
switch(choice){
         case ADD: // you addition set called here break;
         case SUB: // you subtraction set called here break;
         // etc.
         default: cout <<"Error has occured in selection"; break;


make sure to keep a break at the end of each case and to receive your variables before you call the function. But that should get you to the solution.
Just a healthy reminder:

Never use the goto statement.

It's bad programming practice.
Topic archived. No new replies allowed.