算术操作的C 开关语句

C++ switch statement for arithmetic operations

本文关键字:开关 语句 操作      更新时间:2023-10-16

当我运行程序时,它不允许我选择操作。它只是直接进入"无效的选项",然后再次询问。我希望能够选择' ',',','*','/','^'和'!''作为我的选择。我做错了什么?

#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 
    << " + additionn - subtractionn * multiplicationn / divisionn ^ number to powern ! 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 << " + additionn - subtractionn * multiplicationn / divisionn ^ number to powern ! factorial" << "n q quit" << endl << endl << "Operation? ";
cin >> operation; 
return 0;
} //main statement closed

您使用的是charint s的开关,该开关与您希望选择的字符代码不匹配。如果您想在字符之间做出决定,请使用字符文字而不是整数数字:

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;