简单的c++连续计算器,无法退出程序

Simple c++ continuous calculator, can't exit the program

本文关键字:退出程序 计算器 c++ 连续 简单      更新时间:2023-10-16

我在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);