0

For some reason, it won't print my return statements I have tried every thing, but I just can't get it right.

//calculator
#include <iostream>
using namespace std;
int input1;
int input2;

int add(int input1, int input2)
{
    cout<<"Enter two numbers to add: ";
    cin>> input1,input2;
    return  (input1 +  input2);
}
int subtract(int input1, int input2)
{
    cout<<"Enter first number to subtract: ";
    cin>> input1;
    cout<<"Enter second number to subtract: ";
    cin>> input2;
    return (input1 -  input2);
}
int multiply(int input1, int input2)
{
cout<<"Enter two numbers to multiply: ";
cin>>  input1, input2;
return (input1 * input2);
}

int main()
{
    cout<<"what do you want to do: ";
    int selection;
    cout<<"1.add\n";
    cout<<"2.subtract\n";
    cout<<"3.multiply\n";
    cin>>selection;
    if (selection ==  1) {
        return add(input1, input2);
        return input1 + input2;
    }
    else if (selection ==  2) {
        return subtract(input1, input2);
        return input1 - input2;
    }
    else if (selection ==  3) {
        return multiply( input1, input2);
        return input1 * input2;
    }
    else{
        cout<<"Error choice not available";
    }
    cin.get();
    system("pause");
}
phuclv
  • 37,963
  • 15
  • 156
  • 475

4 Answers4

3

"for some reason it won't print my return statements".

It is because you are not printing anything, you just returning the results of the functions from the main.

Here is your problem:

if (selection ==  1) {
    return add(input1, input2);
    return input1 + input2;
    // no printing statment
}
else if (selection ==  2) {
    return subtract(input1, input2);
    return input1 - input2;
    // no printing statment here as well
}
else if (selection ==  3) {
    return multiply( input1, input2);
    return input1 * input2;
    // nither here 
}

you should print this way:

if (selection ==  1) {
    cout << add(input1, input2) << endl;
}
else if (selection ==  2) {
    cout << subtract(input1, input2) << endl;
}
else if (selection ==  3) {
    cout << multiply( input1, input2) << endl;
}

Also you need to get the input from the user like you did at the subtract function i.e. change this:

cout<<"Enter two numbers to add: ";
cin>> input1,input2;

and

cout<<"Enter two numbers to multiply: ";
cin>>  input1, input2;

To this:

cout<<"Enter first number to subtract: ";
cin>> input1;
cout<<"Enter second number to subtract: ";
cin>> input2;
phuclv
  • 37,963
  • 15
  • 156
  • 475
Ran Eldan
  • 1,350
  • 11
  • 25
0

Basically, you don't see your return values because you don't ever print them to the screen. Either do so in each of your routines, or in your main routine.

You may also want to remove all those return statements in your main routine; each one of them immediately ends the running program. (Hint: removing one of each is not enough.)

Jongware
  • 22,200
  • 8
  • 54
  • 100
0

return in main will stop executing the program immediately and return the exit code to the OS. This exit code can be used for error examination, result checking, etc... but the OS won't print it to the console

In your code it will stop after the first function call and the next return statement will never be reached. You'll get many warnings about unreachable code. Always enable all warnings in your compiler and read them, it's very helpful to help you solve lots of bugs

But even if the next statement is reached, nothing will happen except returning the number to the system that you can check by running echo %errorlevel% in cmd or echo $? in bash

See

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

Just to give a slightly more complete explanation - your return statements in main do exactly the same thing they do in any other function - return a value. As Ran Eldan mentioned, you need to use cout to print to the console, like you did to ask for input. When you return an int in main, you are actually return the number to the operating system to use as a program status code to be checked after execution ("0" is generally the code for "everything ran smoothly"). You can actually check this value in a UNIX terminal (Mac OSX or Linux) by using echo $? after the program finishes. With your current program structure, you would be able to get your results by doing this, but that's clearly not what you want.

phuclv
  • 37,963
  • 15
  • 156
  • 475
IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17