Having trouble with variables in different functions for a calculator

Hey guys I am new with C++(started 2 days ago) and I made two different projects for a simple calculator with very basic arithmetic expressions, the first project worked out fine because I made it all in one function but in the second one I tried going with various functions that would solve the expressions separately. I dont know what I am doing wrong, I think it might be something with the variables in between the main function and the others. Any help would be appreciated, thanks for reading.
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
 #include <iostream>

void addition(int a, int b)
{
    int add;
    add = a+b;
    std::cout << "The answer is " << std::endl << add << std::endl;
}
void subtraction(int a, int b)
{
    int sub;
    sub = a-b;
    std::cout << "The answer is " << std::endl << sub << std::endl;
}
void multiplication(int a, int b)
{
    int pro;
    pro = a*b;
    std::cout << "The answer is " << std::endl << pro <<std::endl;
}

void division(int a, int b)
{
    int div;
    div = a/b;
    std::cout << "The answer is " << std::endl << div << std::endl;
}


int main()
{
    int x;
    int a;
    int b;
    

    
    std::cout << "Choose the operation: +:1, -:2, *:3, /:4" << std::endl;
    std::cin >> x;
    std::cout << "Type the first number" << std::endl;
    std::cin >> a;
    std::cout << "Type the second number" << std::endl;
    std::cin >> b;
    
    
    
    if (x==1)
    {
        addition(a, b);
    }
    else if(x==2)
    {
        subtraction(a, b);
    }
    else if(x==3)
    {
        multiplication(a, b);
    }
    else if(x==4)
    {
        division(a, b);
    }
    

    return 0;
}
I compiled and executed. It seems to work fine.
oh I think it was just an issue with something in xcode, the code seems to be fine.
Last edited on
if your code is doing weird, unexpected behaviour, try another compiler. There's an online one here: http://cpp.sh/.

Another troubleshooting method is to wipe out your .o, obj, or intermediate files and do a clean build.
Hi,

Good work ! Not a bad effort for 2 days coding :+) I really like you are using std:: already :+D

However, a couple of problems with the division part.

First is that the types are int, integer division doesn't help you here. 2/4 is 0 and 25/13 is 1. So you could make the types double, that will solve that problem.

Next, it's always a good idea to check for division by zero before you do any division, otherwise you will have infinity. With a double this is a little more tricky, doubles are stored as binary fractions and are not exact, so we shouldn't compare them using ==, but we can use relational operators < and > and <= etc. Instead we see if it's absolute value is less than some precision value we choose. So it is "near enough" to zero for our purposes. Consider this snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cmath>

// ...
// ...

double a = 0.1; // really 0.09999999999997 say, or 0.10000000000002
double b = 1.0 - (10.0 * a) ;  // 3e-16 say if(b == 0.0) is false

const double PRECISION = 1e-4;  // 0.0001 change this to whatever suits you
bool IsNearToZero = false;

if (std::abs(b) < PRECISION) { 
   std::cout << "b is near enough to zero, using " << PRECISION << " as precision\n";
   IsNearToZero = true;
}


Good Luck !! And don't hesitate to ask questions :+)
Topic archived. No new replies allowed.