简单的c++连续计算器,无法退出程序
Simple c++ continuous calculator, can't exit the program
我在C++制作了这个简单的计算器。它连续接受输入,但我在将操作变量定义为 char 时遇到了麻烦,因为它让我无法退出程序。代码如下:
#include <iostream>
int main(){
long double num1, num2; char operation;
std::cout << "Welcome to the calculator!nnInput numbers and operations to begin. (2+2) Then hit enter.nnThe calculator will continue to expect an operation and number (e.g. +6)nuntil you enter "q" as an operation.nnEnter "q" as an operation to quit.nn";
std::cin >> num1 >> operation >> num2;
do{
switch(operation){
case '+':
num2 += num1;
break;
case '-':
num2 -= num1;
break;
case '/':
num2 /= num1;
break;
case '*':
num2 *= num1;
break;
default:
std::cout << "Not a valid operation. Try again.";
break;
}
std::cout << num2;
} while (std::cin >> operation >> num1);
return 0;
}
该程序运行良好且运行良好,我只是不知道如何退出。我试过让"q"成为返回 0 的情况,但它似乎不起作用,因为我的 do-while 期望 2 个输入......有什么想法或想法吗?
std::cin >> num1 >> operation >> num2;
do
{
switch(operation){
case '+': num2 += num1; break;
case '-': num2 -= num1; break;
case '/': num2 /= num1; break;
case '*': num2 *= num1; break;
default: std::cout << "Not a valid operation. Try again."; break; }
std::cout << num2;
std::cin >> operation;
if (operation == 'q') break;
else std::cin >> num1;
}
while (1);
示例 I/O3+47+3303结束3+4
这是我
的看法。
int main()
{
long double num1, num2;
char operation;
bool stop = false;
std::cout << "Welcome to the calculator!nnInput numbers and operations to begin. (2+2) Then hit enter.nnThe calculator will continue to expect an operation and number (e.g. +6)nuntil you enter "q" as an operation.nnEnter "q" as an operation to quit.nn";
std::cin >> num1 >> operation >> num2;
do{
switch(operation) {
case '+':
num2 += num1;
break;
case '-':
num2 -= num1;
break;
case '/':
num2 /= num1;
break;
case '*':
num2 *= num1;
break;
default:
std::cout << "Not a valid operation. Try again.";
break;
}
std::cout << num2 << std::endl;
// Skip till the end of the line.
while ( std::cin.get() != 'n');
// Read the next operation.
std::cin >> operation;
if ( operation == 'q' )
{
stop = true;
}
else
{
std::cin >> num1;
}
} while (std::cin.good() && !std::cin.eof() && !stop);
return 0;
}
case 'q': exit(0);
或
case 'q': std::exit;
不要忘记添加 cstdio 头文件。
尝试这样做。
std::cin >> num1 >> operation >> num2;
do{
std::cout << "Enter operation";
std::cin >>operation;
switch(operation){
case '+':
num2 += num1;
break;
case '-':
num2 -= num1;
break;
case '/':
num2 /= num1;
break;
case '*':
num2 *= num1;
break;
case 'q':
return 0; //or exit(0);
default:
std::cout << "Not a valid operation. Try again.";
break;
}
std::cout << num2;
} while (true);
相关文章:
- 为什么异常不退出程序?
- 虽然循环在使用回车键时不退出程序
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 捕获异常后如何退出程序执行
- 在C++中使用exit()退出程序有问题吗
- 从函数打印 CLI 帮助消息后,我应该如何干净地退出 C++ 程序?
- 如何实现否定用户输入退出程序和打印列表?
- gdb 退出:程序接收信号 SIGILL,使用 gcc / intel_mkl / gfortran -mavx 编译的
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 如何在C 中按ENTER/返回键时退出程序循环
- 退出程序,默认开关
- 我想让程序保持运行,直到用户输入 n 退出程序
- 我找不到第一个用户输入后立即退出程序的原因
- 安全地退出程序 Win32 C++从分离的 std::线程
- 如何使用QCloseEvent退出程序
- 在主之前退出程序是明确的行为吗?
- C++键盘挂钩-退出程序,但也防止密钥在其他地方被处理
- 在类中运行析构函数时退出程序
- 由于删除 [] 而未退出程序
- 简单的c++连续计算器,无法退出程序