c++ - 我的 C++ 计算器似乎出了什么问题?

标签 c++

#include <iostream>

using namespace std;

int main()
{
int num1,num2,answer;
char choice = 'Y',input;

while (choice == 'Y' || choice == 'y')
{
    cout << "Enter the first number: " << endl;
    cin >> num1;
    cout << "Enter the second number: " << endl;
    cin >> num2;

    cout << "What operation would you like to use?" << endl << endl;
    cout << "Press [A] if you want to use Addition." << endl;
    cout << "Press [S] if you want to use Subtraction." << endl;
    cout << "Press [M] if you want to use Multiplication." << endl;
    cout << "Press [D] if you want to use Division." << endl;

    switch(input)
    {
        case 'A':
        case 'a':
            {
                answer=num1+num2;
                cout << "This is the sum of your equation: " << answer << endl;
                break;
            }
        case 'S':
        case 's':
            {
                answer=num1-num2;
                cout << "This is the difference of your equation: " << answer << endl;
                break;
            }
        case 'M':
        case 'm':
            {
                answer=num1*num2;
                cout << "This is the product of your equation: " << answer << endl;
                break;
            }
        case 'D':
        case 'd':
            {
                answer=num1/num2;
                cout << "This is the quotient of your equation: " << answer << endl;
                break;
            }
        default:
            {
                cout << "Invalid Operation..." << endl;
                break;
            }
    }
    cout << "Do you want to go again? (Y/N) " << endl;
    cin >> choice;
}

cout << "See you later." << endl;

return 0;
}

所以我大约一个半月前刚刚开始上大学,我想我应该尝试一下还没有教给我们的代码。但我遇到了一个问题,每当我构建程序时,它都没有显示错误。但它并没有达到我的预期,即成为一个计算器。它立即跳到:“你想再去一次吗?”输入这两个数字后,它甚至不让用户选择操作,更不用说计算它了。我的代码似乎有什么问题?

[编辑] 我忘了输入 cin >> 输入;在询问要使用哪个操作之后。

最佳答案

正如评论所建议的,您需要在某个时刻获取 input 变量的值。我建议在依赖于它的switch之前:

    cin >> input; // You forgot to put this line in, I think!
    switch(input)
    {
        ...

关于c++ - 我的 C++ 计算器似乎出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58304297/

相关文章:

c++ - 如何从 C++ 共享对象引用调用应用程序

c++ - 如何让 Ragel 执行不同的解析操作

c++ - 在 OpenCV 中寻找轮廓?

c++ - Lua就地初始化表顺序

c++ - 将 std::function 代理到需要参数数组的 C 函数

c++ - 为什么这两个函数不能重载呢?

c++ - 什么是 Microsoft Visual Studio 等效于 GCC ld 选项 --whole-archive

c++ - QTableView 不显示任何内容

c++ - 取消设置最右边的设置位

c++ - array placement new 结合_aligned_malloc,删除的正确方法是什么?