算术运算的C ++ switch语句

时间:2017-03-10 23:19:06

标签: c++ math arithmetic-expressions

当我运行我的程序时,它不允许我选择一个操作。它只是直接进入"无效选项"再问一遍。我希望能够选择' +' - ' - ',' *',' /',&#39 ; ^'和'!'作为我的选择。我做错了什么?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

char operation;
int num1, num2, result, remain;

//Display the menu to the user and get their first choice

    cout << "What operation would you like to perform:" << endl 
    << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" 
    << "\n q quit" << endl << endl << "Operation? ";
    cin >> operation;

//Switch - the user does not want to quit

switch (operation)
{

case 1: result=num1+num2;
    cout << "Enter the first number to add: " << endl;
    cin >> num1;
    cout << "Enter the second number to add: " << endl;
    cin >> num2;
    cout << endl << num1 << " + " << num2 << " = " << result;
    break;

case 2: result=num1-num2;
    cout << "Enter the first number to subtract: " << endl;
    cin >> num1;
    cout << "Enter the second number to subtract: " << endl;
    cin >> num2;
    cout << endl << num1 << " - " << num2 << " = " << result;
    break;

case 3: result=num1*num2;
    cout << "Enter the first number to multiply: " << endl;
    cin >> num1;
    cout << "Enter the second number to multiply: " << endl;
    cin >> num2;
    cout << endl << num1 << " * " << num2 << " = " << result;
    break;

case 4: result=num1/num2;
    cout << "Enter the dividend: " << endl;
    cin >> num1;
    cout << "Enter the divisor: " << endl;
    cin >> num2;
    cout << endl << num1 << " / " << num2 << " = " << result;
    cout << endl << num1 << " % " << num2 << " = " << result;
    break;

case 5: result=num1^num2;
    cout << "Enter the base number " << endl;
    cin >> num1;
    cout << "Enter the power: " << endl;
    cin >> num2;
    cout << endl << num1 << " ^ " << num2 << " = " << result;
    break;

case 6: result=num1!=num2;
    cout << "Enter a number: " << endl;
    cin >> num1;
    cout << endl << num1 << " ! " << " = " << result;
    break;

default: 
    cout << "That is an invalid operation!" << endl;    
    break;

} // switch statement closed

cout << endl << "What operation would you like to perform:" << endl << " + addition\n - subtraction\n * multiplication\n / division\n ^ number to power\n ! factorial" << "\n q quit" << endl << endl << "Operation? ";
cin >> operation; 

return 0;
} //main statement closed

1 个答案:

答案 0 :(得分:3)

您使用的char针对的int开关与您要选择的字符代码不匹配。如果您想在字符中做出决定,请使用字符 literals 而不是整数:

case '+':
    // Read inputs first
    cout << "Enter the first number to add: " << endl;
    cin >> num1;
    cout << "Enter the second number to add: " << endl;
    cin >> num2;
    // Compute the result next
    result=num1+num2;
    cout << endl << num1 << " + " << num2 << " = " << result;
    break;

请注意作业

result=num1+num2;

读取输入的代码之后,即num1num2

  

当我问它是否想要做另一个操作时,即使我选择了一个操作,它也会结束。

这是因为您在num2之后输入的行尾字符位于缓冲区中。你需要忽略它。添加以下行:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

cin >> operation;