Basic Calculator with Functions

I was in the process of making a basic calculator, when my friend mentioned that using int addition(), int subtraction, etc... to list the different functions, would be a much cleaner and easier way to go about this. I had the calculator working, but when I try this format it doesn't seem to turn out any results. Can someone shed light on why this won't run?

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

using namespace std;

int addition()
{
    double a, b;
    double result;

    cout << "\nInput your 1st number: ";
    cin >> a;
    cout << "\nInput your 2nd number: ";
    cin >> b;
    result = a + b;
    cout << "\n\nResult: "
    << result;
}

int subtraction()
{
    double a, b;
    double result;

    cout << "\nInput your 1st number: ";
    cin >> a;
    cout << "\nInput your 2nd number: ";
    cin >> b;
    result = a - b;
    cout << "\n\nResult: "
    << result;
}

int multiplication()
{
    double a, b;
    double result;

    cout << "\nInput your 1st number: ";
    cin >> a;
    cout << "\nInput your 2nd number: ";
    cin >> b;
    result = a * b;
    cout << "\n\nResult: "
    << result;
}

int division()
{
    double a, b;
    double result;

    cout << "\nInput your 1st number: ";
    cin >> a;
    cout << "\nInput your 2nd number: ";
    cin >> b;
    result = a / b;
    cout << "\n\nResult: "
    << result;
}

int invalid()
{
    cout << "Invalid input.";
    system("cls");
    goto main();
}

int main()
{
    int a;

    cout << "Welcome to Calculator 1.1"
    << "\n--------------------------"
    << "\n\nSelect your function:\n\n"
    << "1. Addition\n"
    << "2. Subtraction\n"
    << "3. Multiplication\n"
    << "4. Division\n\n"
    << "Selection: ";
    cin >> a;
    cout << "\n--------------------------";

    if (a == 1)
        {
            goto addition();
        }

    if (a == 2)
        {
            goto subtraction();
        }

    if (a == 3)
        {
            goto multiplication();
        }

    if (a == 4)
        {
            goto division();
        }

    if (a != 1, 2, 3, 4)
        {
            goto invalid();
        }
}


Also, it states, "error: system is not declared in this scope." Is there a way to fix this as well?

Any suggestions/advice on how to improve the program is highly appreciated! Thanks guys!
Well, first of all. In all your functions you are returning a value, when you still have instructions. The last two lines of all your computation functions are never even read. This is why results are never shown.

Why on earth are you using goto?? Especially so often. Do you have a problem with calling functions normally?

System calls are bad practice. I'm not sure who always tells people to use there....? Get rid of that and your error will be gone. Just out of curiosity though, what OS are you on?

Your last if, could just be changed to an else. Would be much cleaner and a little better performance wise.
Hey ResidentBiscuit,
I'm very new. Like, a week into self-teaching myself. Thus the reason I'm so behind in progression. And the reason I have a bunch of questions on what you said.

"In all your functions you are returning a value, when you still have instructions. The last two lines of all your computation functions are never even read. This is why results are never shown."
- This isn't true... I don't think. It worked for me when I had it in the old format.

"Why on earth are you using goto?? Especially so often. Do you have a problem with calling functions normally?"
- I didn't know there was another way :/ Can you explain? I'm gonna be honest, I just got to the functions portion of cplusplus.com's tutorial.

"System calls are bad practice. I'm not sure who always tells people to use there....? Get rid of that and your error will be gone. Just out of curiosity though, what OS are you on?"
- I've heard that about system calls, but besides "system('cls')" do I use it a lot? :/ Sorry. And operating system? Windows 7. CodeBlocks for my compiler.

And the last part about the if to else makes sense! Much obliged if you could answer these!
Oh sorry that says "result", not return. Unless you changed it? I don't remember at this point. I can guarantee that a function is done once it hits a return, if its a non void function that is. Return is the exit point of any function. Main() for example, that is the entry point to your program. And once main hits a return, your program is done.

Anyways, I think you changed your functions. Cause now there is no return statement at all. If you have a function of any data type (ie not void), you have to have a return in there. The compiler is expecting a value to be kicked out of there.

In regards to goto, I've never actually seen it used as a function call before. I wonder if someone with more experience could explain why this is a bad idea?

sampleFunction(); This is how you call a function. All you do is write it. So just delete your all of your goto. If your function ever has any parameters (Stuff in parentheses), then you have to send arguments with matching data type.

No, that's the only system call I see in there. There's many articles on why it's bad to use, and you can look it up yourself if you're curious. There's other ways to clear the screen, that are much safer and efficient.
Code::Blocks is your IDE, MingW is your compiler. There is a difference.

You have a few mistakes in there, which is normal for being a beginner. So if you fix your function calls, and put in proper returns, this will look much nicer. You have a goto main() in there. I never use goto, and wouldn't even think about using it to goto main(), can you tell me if your invalid() function actually works? I know you can't call main from anywhere in your program, but I dont know about using goto. But, thats just my curiosity, I can test it myself actually. Get rid of all goto's. This will have you rethink your logic a bit, because you are no longer allowed to just restart main(). Look into while loops.

-Biscuit
Something like goto addition(); shouldn't compile since goto needs a label not a function. Remove all goto expressions.

This if (a != 1, 2, 3, 4) doesn't do what you think it does. Each expression before the comma is evaluated and the last one is used for the operation itself so at the end it's just if (a != 4)

Don't call main(). The task of main is to initialize global variables and anything that's needed to start the program.

This int division() is a function that returns an int. Therefore somewhere (preferable at the end) there must be a return statement.

it doesn't seem to turn out any results
This is due to line 103. if you clear the screen (on line 64) you won't see anything and (like stated above) you call invalid() whenever a != 4

Really the goto thing shouldn't compile. My code::blocks gives me errors like
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void test()
{
  std::cout << "surprise";
}

int main()
{
  goto test();

  std::cout << "Hello World";
}

D:\temp\temp\test\main.cpp||In function `int main()':|
D:\temp\temp\test\main.cpp|10|error: expected `;' before '(' token|
D:\temp\temp\test\main.cpp|10|error: expected primary-expression before ')' token|
D:\temp\temp\test\main.cpp|10|error: label `test' used but not defined|
||=== Build finished: 3 errors, 0 warnings ===|

"I was in the process of making a basic calculator, when my friend mentioned that using int addition(), int subtraction, etc... to list the different functions, would be a much cleaner and easier way to go about this." Using the switch statement would actually be easier, here is my calculator, it does everything yours does :)


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

using namespace std;

int main()
{
    double calc1, calc2;
    char symbols;
    int x = 0;

    cout << "Welcome to the calculator, please enter 2 values." << endl;
    cout << "\n";
    cout << "You can use +, *, / and -" << endl;

    while(x != 1){
    cin >> calc1 >> symbols >> calc2;

    switch(symbols){
        case '+':
            cout << "Equals: " << calc1 + calc2 << "\n" << endl;
            break;
        case '-':
            cout << "Equals: " << calc1 - calc2 << "\n" << endl;
            break;
        case '*':
            cout << "Equals: " << calc1 * calc2 << "\n" << endl;
            break;
        case '/':
            cout << "Equals: " << calc1 / calc2 << "\n" << endl;
            break;
        }
    }
}


Theres a few bugs but you can play around with it if you want.
Last edited on
Thanks guys! I finally got it working the way I wanted, but I got one more little bug that's killing me. This is the basis of what my functions look like now:
1
2
3
4
5
6
7
8
9
10
11
12
13
int division()
{
    double a, b;
    double result;

    cout << "\nInput your 1st number: ";
    cin >> a;
    cout << "\nInput your 2nd number: ";
    cin >> b;
    result = a / b;
    cout << "\n\nResult: ";
    return (result);
}


But when it returns the value, (to be printed back in the main()) it won't show decimals even though I'm using double as the variable. Is there a problem with transitioning from function to function with decimals? Or am I missing something?

Same with the square root function I just included since I found the <math.h> library,
Last edited on
It's because your function is of type int. So it's gonna truncate off the decimal portion. Change your function to double or float
Derp. I can't believe I didn't see that one for myself XD
Oh well. I feel for a week or so of reading, I've come a ways.

Thanks so much ResidentBiscuit! :3
No problem. We were all beginners at one point.
Topic archived. No new replies allowed.