Beginner C++ calculator I'm relatively new to programming, but am really enjoying it. I have taken a couple of classes but mostly code for fun (for now). I decided to make a program implementing some basic functions, switch statements, and user input. Please leave any feedback on how to make this program better.

I understand that I am not actually using the defined functions (add, subtract, etc.), but I put them in there with the intent of using them, I just don't know how.

#include &lt;iostream&gt;
using namespace std;

int add (int x, int y){
return x+y;
};

int divide (int x, int y){
return x/y;
}

int multiply (int x, int y){
return x*y;
}

int subtract (int x, int y){
return x-y;
}


int main(){

int n1;
int n2;
int user14 = 0;

SomeLine:
cout &lt;&lt; "Enter your 2 numbers: "&lt;&lt; endl;
cin &gt;&gt; n1;
cin &gt;&gt; n2;


cout &lt;&lt; "Ok, now what do you want to do with those numbers? "&lt;&lt; endl;

cout &lt;&lt; "1) Add: " &lt;&lt; endl;
cout &lt;&lt; "2) Divide: "&lt;&lt;endl;
cout &lt;&lt; "3) Multiply: "&lt;&lt; endl;
cout &lt;&lt; "4) Subtration: "&lt;&lt; endl;

cin &gt;&gt; user14;

switch (user14)
{
    case 1:
    cout &lt;&lt; n1+n2 &lt;&lt; endl;
        break;
    case 2:
    cout &lt;&lt; n1/n2 &lt;&lt; endl;
        break;
    case 3:
    cout &lt;&lt; n1*n2&lt;&lt; endl;
        break;
    case 4:
    cout &lt;&lt; n1-n2 &lt;&lt; endl;
        break;

}

char userchoice;
cout &lt;&lt; "Would you like to perform any other operations? y/n "&lt;&lt; endl;

cin &gt;&gt; userchoice;

if (userchoice=='y'){
    goto SomeLine;
}
else if(userchoice=='n'){
    goto Exit;
}

Exit:
return 0;

};
Welcome! Here is some feedback, in no particular order.

Avoid using namespace std

This advice is often repeated for good reason. See also: code guidelines SF.6. It's useful when you're learning the C++ standard library to know what belongs in the standard library and what doesn't.

cout &lt;&lt; "Enter your 2 numbers: "&lt;&lt; endl;


Becomes

std::cout &lt;&lt; "Enter your 2 numbers: "&lt;&lt; std::endl;


Consistent spacing

Maybe this is caused by the formatting on Stack Exchange. There are many online style guides, I would choose one that is most readable to you and your peers and stick with it throughout the source code.

int add (int x, int y){
return x+y;
};


Becomes

int add(int x, int y) {
    return x + y;
};


Semicolons

Functions don't need semicolons at the end (main &amp; add). This is only used for function prototypes.

int add(int x, int y) {
    return x + y;
};


Becomes

int add(int x, int y) {
    return x + y;
}


Avoid std::endl

Unless you need to flush the stream, using std::endl can be replaced with a newline character.

std::cout &lt;&lt; "Enter your 2 numbers: "&lt;&lt; std::endl;


Becomes

std::cout &lt;&lt; "Enter your 2 numbers:\n";


Combine multiple std::cout and std::cin calls.

This is just my preference. It reduces the amount of code I have to write.

std::cin &gt;&gt; n1;
std::cin &gt;&gt; n2;


Becomes

std::cin &gt;&gt; n1 &gt;&gt; n2;


And

std::cout &lt;&lt; "1) Add: " &lt;&lt; std::endl;
std::cout &lt;&lt; "2) Divide: "&lt;&lt; std::endl;
std::cout &lt;&lt; "3) Multiply: "&lt;&lt; std::endl;
std::cout &lt;&lt; "4) Subtration: "&lt;&lt; std::endl;


Becomes

std::cout &lt;&lt; "1) Add:\n2) Divide:\n3) Multiply:\n4) Subtration:\n";


In the above example, there are many other ways to do it. For example, you can use multi-line strings, adding the strings, and so on.

Make use of your functions

You define the add, divide, multiply and subtract functions, but you don't call them. Here is how you would do that:

std::cout &lt;&lt; n1 + n2 &lt;&lt; '\n';


Becomes

std::cout &lt;&lt; add(n1, n2) &lt;&lt; '\n';


It's as simple as that!

In complex applications, splitting things into functions makes the code easier to work with. For example, say you wanted the "add" operation to have saturating arithmetic (doesn't overflow). It's easier to add that logic to a separate function rather than inserting it into a long block of code.

Or, if you want to handle a division-by-zero case.

Don't use goto

While goto is useful in a few cases, it's not particularly useful here. Instead you can use a do-while loop.

SomeLine:

// code

if (userchoice == 'y'){
    goto SomeLine;
}
else if(userchoice == 'n'){
    goto Exit;
}

Exit:
return 0;


Becomes

do {
     // code
} while (userchoice == 'y');

return 0;


Variable naming

The variables x and y are used in the function signatures. You can also use those names in main. I'd prefer that over n1 and n2. Likewise, user14 is vague (do we change it to user15 if we include another operation?). Something like operation or op isn't vague. userchoice is a bit long, exit is shorter and clearly shows its purpose.

const

Make use of the const keyword where it makes sense. For example, in add, we intend for the x and y variables to never themselves be modified. So we can declare them as const, and the compiler will give us an error if we accidentally change their values.

This isn't particularly useful anything other than readability here, but can be useful if you get into more advanced topics, such as const references.

The functions add, divide, multiply, and subtract can be constexpr. The compiler may already evaluate these at compile time, but using constexpr marks them as such (broadly speaking).

Move variables to the lowest scope

Now that we've gotten rid of the gotos, we can move x, y, and op to a lower scope. They can also all be declared on the same line.

Consider edge cases

The following are a few example cases where you may wish to add extra handling -- or leave as it currently is.


What if the user enters a letter where you're expecting an integer?
What if the user enters an operation number outside of the range?
What about possible signed integer overflow (undefined behavior)?
What about division-by-zero?
What if the input stream is no longer valid after user input?
...


Conclusion

Here is the code I ended up with:

#include &lt;iostream&gt;

constexpr int add(const int x, const int y) {
    return x + y;
}

constexpr int divide(const int x, const int y) {
    return x / y;
}

constexpr int multiply(const int x, const int y) {
    return x * y;
}

constexpr int subtract(const int x, const int y) {
    return x - y;
}


int main() {
    char exit;

    do {
        int x, y, op;

        std::cout &lt;&lt; "Enter your 2 numbers:\n";

        std::cin &gt;&gt; x &gt;&gt; y;

        std::cout &lt;&lt; "Ok, now what do you want to do with those numbers?\n";
        std::cout &lt;&lt; "1) Add:\n2) Divide:\n3) Multiply:\n4) Subtration:\n";

        std::cin &gt;&gt; op;

        switch (op) {
            case 1:
                std::cout &lt;&lt; add(x, y) &lt;&lt; '\n';
                break;
            case 2:
                std::cout &lt;&lt; divide(x, y) &lt;&lt; '\n';
                break;
            case 3:
                std::cout &lt;&lt; multiply(x, y) &lt;&lt; '\n';
                break;
            case 4:
                std::cout &lt;&lt; subtract(x, y) &lt;&lt; '\n';
                break;
        }

        std::cout &lt;&lt; "Would you like to perform any other operations? y/n\n";

        std::cin &gt;&gt; exit;
    } while (exit == 'y');

    return 0;
}


Hopefully this has been helpful. I hope you continue to enjoy programming as much as we all do!