-2

I have a basic menu program that gives the user different programs to choose from and run, when I run the simpleCalculator option and iput values and the operation of arithmetic the output is always the wrong answer but when I first programmed the calculator program without the menu and it being in its own function it would run just fine and give the the right answers, what exactly is going wrong here? Ignore the third option in the menu since i haven't added code for the prime number checker program

double first_arg;
double second_arg;
string arithmeticOperation;
int numBottles = 99;

double simpleCalculator()
{

    cout << "Enter first value: ";
    cin >> first_arg;
    cout << "Enter choice of arithmetic operation:\n* = multiplication\n/ = division\n+ = addition\n- = subtraction\n\n";
    cin >> arithmeticOperation;
    cout << "\nEnter second value: ";
    cin >> second_arg;

    if(arithmeticOperation == "*")
    {
        cout << "\nYour answer is " << first_arg * second_arg;
    }
    else if(arithmeticOperation == "/")
    {
        cout << "\nYour answer is " << first_arg / second_arg;
    }
    else if(arithmeticOperation == "+")
    {
        cout << "\nYour answer is " << first_arg + second_arg;
    }
    else if(arithmeticOperation == "-")
    {
        cout << "\nYour answer is " << first_arg - second_arg;
    }
    else
    {
        cout << "\nPlease enter a valid choice of an arithmetic operation\n";
    }
}

string bottlesProgram()
{
    while(true)
    {

        if (numBottles > 2)
        {
            cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
            cout << "\n\n";
        }

        else if(numBottles == 2)
        {
            cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottle of beer on the wall.";
            cout << "\n\n";
        }
        else if(numBottles == 1)
        {
            cout << numBottles << " bottle of beer on the wall\n" << numBottles << " bottle of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
            cout << "\n\n";
        }
        else if(numBottles == 0)
        {
            cout << "No more bottles of beer on the wall,\nNo more bottles of beer.\nGo to the store and buy some more,\n" << "99 bottles of beer on the wall...";
            cout << "\n\n";
        }
        else
        {
            break;
        }
        numBottles--;
    }
}

int main()
{
    int menuInput;
    cout << "           ***Programs Menu***\n" << "------------------------------------------\n" << "1. Run a simple Calculator\n" << "2. Run 99 bottles of beer on the wall song\n" << "3. Run prime number checker program\n" << "------------------------------------------\n";
    cin >> menuInput;
    if(menuInput == 1)
    {
        cout << simpleCalculator();
    }
    else if (menuInput == 2)
    {
        cout << bottlesProgram();
    }

}
double-beep
  • 5,031
  • 17
  • 33
  • 41
jcode
  • 3
  • 3
  • 2
    You need to return something from the functions. – David Ranieri Dec 27 '20 at 06:00
  • yeah that's what I was confused about but all solved now, thanks – jcode Dec 27 '20 at 06:19
  • If the problem is "solved now", can you please self-answer the question with explanation of what was wrong and how to fix that, so that archiving this question can be useful for others? Thanks! – exa Dec 27 '20 at 10:14

1 Answers1

2

You seem to be confused about returning values from a function, and printing values in a function. These are not the same thing.

If you print the values in the function then the function should be void (i.e. nothing is returned) and the printing happens inside the function (obviously)

void simpleCalculator()
{
    ...
    cout << "\nYour answer is " << first_arg * second_arg;
    ...
}

simpleCalculator(); // no cout

On the other hand if you return a value from the function then the function is not void and the printing happens outside the function

double simpleCalculator()
{
    ...
    return first_arg * second_arg; // return not cout
    ...
}

cout << "\nYour answer is " << simpleCalculator();

Your code is doing half one version and half the other.

john
  • 85,011
  • 4
  • 57
  • 81
  • Just a warning for future readers: If you do it the second way, make sure all execution paths return a value, otherwise your program will have [Undefined Behavior](https://stackoverflow.com/q/32132574/10957435). –  Dec 28 '20 at 03:14