算术操作的C 开关语句
C++ switch statement for arithmetic operations
当我运行程序时,它不允许我选择操作。它只是直接进入"无效的选项",然后再次询问。我希望能够选择' ',',','*','/','^'和'!''作为我的选择。我做错了什么?
#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
您使用的是char
的int
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;
应在之后读取输入的代码,即 num1
和 num2
。
最后,当我询问它是否要执行另一个操作时,即使我选择了一个操作,它也会结束。
这是因为您在 num2
坐在缓冲区中输入的一个线结束字符。您需要忽略它。添加此行:
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
之前 cin >> operation;
相关文章:
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 为什么布尔开关语句有编译器警告?
- 如何使用"equal to"以外的评估编写开关语句
- 有没有办法在C++将字符串与开关语句一起使用?
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 如何循环开关语句直到找到所需的数字?
- 带有关闭/打开布尔值的开关语句
- 清理开关语句
- 我的开关语句在C++中不起作用
- 枚举和开关语句C++
- 如何在开关语句中使用 cin "as a parameter"
- "constexpr if"比开关语句好吗?
- C++ 默认值在开关语句中不起作用
- 初学者C :为什么此开关语句给我一个错误
- C 开关语句无法正常工作
- 为什么允许在开关语句中声明变量?但不是声明 初始化
- 根据数据包类型更改行为,避免开关语句
- 如何在开关语句(C )内或外部使用IF语句
- 使用开关语句案例中的随机数