0

Im a college student so I made a simple calculator using functions, and its working. but after i put the 2 numbers in, it wouldn't let me choose what option i wanted to choose. i also got a WARNING C4715, but the code still runs properly... I tried reversing the order of the code, but obviously that failed. can anyone help :( ???

CODE:

#include<iostream>

using namespace std;

double operation(double num1, double num2)
{
    cout << "Press 1 for addition" << endl;
    cout << "Press 2 for subtraction" << endl;
    cout << "Press 3 for multiplication" << endl;
    cout << "Press 4 for division" << endl;
    cout << "Press 5 for raise to the power of x" << endl;
    cout << "Press 6 for Even or Odd" << endl;
    cout << "Press 7 for Quit" << endl;
    int Option{};
    switch (Option)
    {
    case 1:
    {
        return num1 + num2;
        cout << "The total sum = " << num1 + num2 << endl;
    }
    break;

    case 2:
    {
        return num1 - num2;
        cout << "The total differencr = " << num1 - num2 << endl;
    }
    break;

    case 3:
    {
        return num1 * num2;
        cout << "The total product = " << num1 * num2 << endl;
    }
    break;

    case 4:
    {
        return num1 / num2;
        cout << "The total quotient = " << num1 / num2 << endl;
    }
    break;

    case 5:
    {
        double x = num1;
        cout << pow(x, num2);
        cout << "The Number of" << x << " raised to the power of" << num2 << "is" << pow(x, num2) << endl;

    }
    break;

    case 6:
    {int num1{};

    if (num1 % 2 == 0)
        cout << "The Number Is EVEN.";

    else
        cout << "The Number Is ODD";

    }
    break;

    case 7:
    {
        cout << "You are now Leaving the Calculator, GOODBYE !!! " << endl;

    }
    break;

    default:
    {
        cout << "Invalid Entry!\nPlease re-run the program and " << endl;
        cout << "enter a valid menu choice.\n";

    }
    break;
    }

}

int main()
{
    double num1, num2;
    cout << "Enter a value for num1: " << endl;
    cin >> num1;
    cout << "Enter a second value for num2: " << endl;
    cin >> num2;
    double store = operation(num1, num2);

    system("pause");
    return 0;
}
  • Please reproduce the full warning message in your question and also add the inputs that you used and the outputs that you expected. – walnut Nov 17 '19 at 00:58
  • 1
    Put a `return -1.0;` at the end of the function. – S.S. Anne Nov 17 '19 at 01:01
  • Additionally, your `operation`()'s intentions to print various messages ***after*** `return`ing will not be very productive. And as soon as you find the exact line in your program which attempts to read the user's selection (hint: it's not `int Option{};`), you will figure out why this doesn't work. – Sam Varshavchik Nov 17 '19 at 01:11
  • 1
    Please read your program carefully before asking questions. You are never reading anything into `Option` from the user, before testing it in the `switch` (which is why "*it wouldn't let me choose what option i wanted*"). Also `return` *returns from the function immediately*. Writing code after a return statement in the same control flow block makes no sense (and the compiler should be warning you about unreachable code). – walnut Nov 17 '19 at 01:12
  • Since your function returns a `double`, you need a `default` case that returns a value. What happens when a user enters the value `10`? Your `default` case doesn't return a value, so it `break`s out of the switch to the end of the function, which doesn't return a `double` either. – Thomas Matthews Nov 17 '19 at 01:47

0 Answers0