简体   繁体   English

如何使用基本菜单在 c++ 程序中修复我的计算器答案?

[英]How do I go about fixing my calculator answers in my c++ program with a basic menu?

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?我有一个基本菜单程序,当我运行 simpleCalculator 选项和输入值以及算术运算时,我有一个基本菜单程序,它为用户提供了不同的程序可供选择和运行它在自己的 function 中运行得很好并给出正确的答案,这里到底出了什么问题? 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();
    }

}

You seem to be confused about returning values from a function, and printing values in a function.您似乎对从 function返回值以及在 function 中打印值感到困惑。 These are not the same thing.这些不是一回事。

If you print the values in the function then the function should be void (ie nothing is returned) and the printing happens inside the function (obviously)如果您打印 function 中的值,则 function 应该是void的(即没有返回任何内容)并且打印发生在 function 内(显然)

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另一方面,如果您从 function 返回一个值,则 function 不是void的,并且打印发生在 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.您的代码正在执行一个版本的一半和另一个版本的一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM