C++ arithmetic on two numbers I need someone with experience to judge my first project to let me know what I have to work on. I recently started learning C++ using resources like Solo Learn and Bucky's tutorials on YouTube hoping to eventually gain enough experience to find a job in software development.

#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;
//Functions



// addition function
int calc_sum(){
double x;
double y;
cout &lt;&lt; "enter two numbers to get a sum" &lt;&lt;endl;
cin &gt;&gt; x &gt;&gt; y;
double sum = x+y;
cout &lt;&lt;"The sum is "&lt;&lt; sum &lt;&lt;"."&lt;&lt;endl;

}
// multiplication function
int calc_pro(){
double a;
double b;
cout&lt;&lt;"enter two numbers to find the product"&lt;&lt;endl;
cin&gt;&gt; a &gt;&gt; b;
double product = a * b;
cout&lt;&lt; "The product is "&lt;&lt; product &lt;&lt;"."&lt;&lt;endl;

}
// division function
int calc_div(){
double x;
double y;
cout&lt;&lt; "enter two numbers to divide."&lt;&lt;endl;
cin&gt;&gt; x &gt;&gt; y;
double div = x/y;
cout &lt;&lt; "The quotient is "&lt;&lt; div &lt;&lt;"." &lt;&lt;endl;
}
// Subtraction function
int calc_sub() {
double a;
double b;
cout&lt;&lt; "Enter two numbers to subtract."&lt;&lt; endl;
cin&gt;&gt; a &gt;&gt; b;
double diff = a-b;
cout&lt;&lt; "The difference is "&lt;&lt; diff &lt;&lt;endl;

}

//core function

void core2(){
string choice;
cout&lt;&lt;"Would you like to multiply, divide, add, or subtract? (typer in lowercase)"&lt;&lt;endl;
cin&gt;&gt;choice;

if(choice=="add"){
calc_sum();
core2();
}
else if(choice=="subtract"){
calc_sub();
core2();
}
else if(choice=="multiply"){
calc_pro();
core2();
}
else if(choice=="divide"){
calc_div();
core2();
}
else{
cout&lt;&lt;"USER ERROR"&lt;&lt;endl&lt;&lt; "you typed in something wrong, try again."&lt;&lt;endl;
core2();
}
}







int main(){


core2();



}
Code formatting

Format your code to improve readability and hence maintainability.

DRY (Don't Repeat Yourself) Principle

You have a lot of places with duplicate code. You should try to avoid this.

First of all in your core2 method you have calls in all condition blocks, you should move it below to be executed in any case.

Also you have many duplicate code in your calc methods. You can move duplicate code to your core core2 method.

Inconsistent styles

Use the same style everywhere. If you are starting your sentences for output with uppercase then try to follow it everywhere. The same is true about comments and constructs like to get a sum, to find a product, to divide. Use single ubiquitous style for everything.

Function return types

If you don't want to return anything from your function it should be void. All your calc functions are ints (but you don't call return operator) and you are not using return values. So all your calc functions should be void.

Redundant core2 function

Also core2 function is not needed here, you can use main directly. But C++ specs prohibits this behaviour although this works fine in most compilers. So we can replace core2 recursion with infinite loop in main.



So if we take into account all above notes code will look like this

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

// Now we have only primitive one-liner arithmetic functions
// with double return types    

double add(double a, double b) { return a + b; }

double subtract(double a, double b) { return a / b; }

double multiply(double a, double b) { return a * b; }

double divide(double a, double b) { return a / b; }

void core2() {
    string choice;
    cout &lt;&lt; "Would you like to multiply, divide, add, or subtract? (typer in lowercase)" &lt;&lt; endl;
    cin &gt;&gt; choice;

    // Choosing correct action name to output
    string result_name;

    if (choice == "add") {
        result_name = "sum";
    } else if (choice == "subtract") {
        result_name = "difference";
    } else if (choice == "multiply") {
        result_name = "product";
    } else if (choice == "divide") {
        result_name = "quotient";
    } else { // This block also performs validation of input parameter
        result_name = ""; // Just to prevent compiler error due to undeclared variable
        cout &lt;&lt; "USER ERROR" &lt;&lt; endl &lt;&lt; "you typed in something wrong, try again." &lt;&lt; endl;
        return; // end execution here
    }

    // Entering numbers in single place
    double a;
    double b;
    cout &lt;&lt; "Enter two numbers to get the "&lt;&lt; result_name &lt;&lt; "." &lt;&lt; endl;
    cin &gt;&gt; a &gt;&gt; b;

    double result;

    if (choice == "add") {
        result = add(a, b);
    } else if (choice == "subtract") {
        result = subtract(a, b);
    } else if (choice == "multiply") {
        result = subtract(a, b);
    } else {
        result = divide(a, b);
    }

    // Outputting result in single place
    cout &lt;&lt; "The " &lt;&lt; result_name &lt;&lt; " is " &lt;&lt; result &lt;&lt; "." &lt;&lt; endl;
}

int main() {
    while (true) {
        core2();
    }
}




Further improvement will use more advanced techniques and relatively new C++11 features.

You need some way to map string and function, so for this we will use std::map as dictionary and std::function as polymorphic function wrapper. Also lambdas will be use to pass anonymous functions here as parameters. struct will be here used as container to store calculation function and result description.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;functional&gt;
#include &lt;map&gt;
using namespace std;

struct CalculationTechnique {
    std::function&lt;double (double,double)&gt; Function;
    string ResultName;
};

void core2() {
    string choice;
    cout &lt;&lt; "Would you like to multiply, divide, add, or subtract? (typer in lowercase)" &lt;&lt; endl;
    cin &gt;&gt; choice;

    // Filling our dictionary
    std::map&lt;string, CalculationTechnique&gt; calculation_mapping = {
        { "add", CalculationTechnique { [](double a, double b) -&gt; double { return a + b; }, "sum" } },
        { "subtract", CalculationTechnique { [](double a, double b) -&gt; double { return a - b; }, "difference" } },
        { "multiply", CalculationTechnique { [](double a, double b) -&gt; double { return a * b; }, "product" } },
        { "divide", CalculationTechnique { [](double a, double b) -&gt; double { return a / b; }, "quotient" } }
    };    

    // if no value found in dictionary
    if (calculation_mapping.count(choice) == 0) {
        cout &lt;&lt; "USER ERROR" &lt;&lt; endl &lt;&lt; "you typed in something wrong, try again." &lt;&lt; endl;
        return;
    }

    CalculationTechnique technique = calculation_mapping.at(choice);

    double a;
    double b;
    cout &lt;&lt; "Enter two numbers to get the "&lt;&lt; technique.ResultName &lt;&lt; "." &lt;&lt; endl;
    cin &gt;&gt; a &gt;&gt; b;

    cout &lt;&lt; "The " &lt;&lt; technique.ResultName &lt;&lt; " is " &lt;&lt; technique.Function(a, b) &lt;&lt; "." &lt;&lt; endl;
}

int main() {
    while (true) {
        core2();
    }
}