for some reason my printResult(result) at the end is undefined i need some advice as to what i did wrong
code:
#include <iostream>
int getNumber()
{
int number1;
std::cin >> number1;
std::cout << "choose your first number" << std::endl;
return number1;
}
int operation()
{
int operation;
std::cin >> operation;
std::cout << "pick your operator 1 = + 2 = - 3 = * 4 = /" << std::endl;
return operation;
}
int calculation(int x, int y, int operation)
{
if (operation == 1)
return x + y;
if (operation == 2)
return x - y;
if (operation == 3)
return x * y;
if (operation == 4)
return x / y;
return -1;
}
int printResult(int result)
{
std::cout << "your answer is " << result << std::endl;
}
int main()
{
int input1 = getNumber();
int op = operation();
int input2 = getNumber();
int calc = calculation(input1, op, input2);
This function must return a value, because the function is a int.
1 2 3 4
int printResult(int result)
{
std::cout << "your answer is " << result << std::endl;
}
1 2 3 4 5 6 7 8 9
int main()
{
int input1 = getNumber();
int op = operation();
int input2 = getNumber();
int calc = calculation(input1, op, input2);
printResult(result); // The variable result is undeclared on main function.
}
Since you want to print the result of the calculation, you can call the function print result with the value calc. For instance,
printResult(calc);
Furthermore, make sure you return 0 at the end of the main function. I hope it helps
4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.
5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program)
I agree with you, I will keep using return 0; as well myself. What if the C++ ISO Committee decides that return 0; be mandatory in some future standard?