【问题标题】:C++ Calculator OperatorC++ 计算器运算符
【发布时间】:2019-12-20 03:09:28
【问题描述】:

我的代码有问题,每次我输入除运算符 (+,/,*,-) 以外的其他内容时,它仍然会要求输入数字。我希望它停止并显示文本“错误!运算符不正确”,当有人将其他东西放在运算符之外时

#include <iostream>
using namespace std;
int main()
{
    char op;
    float num1, num2;
    cout << "Enter operator either + or - or * or /: ";
    cin >> op;
    cout << "Enter the first operand: ";
    cin >> num1;
    cout << "Enter the second operand: ";
    cin >> num2;

    switch (op)
    {

    case '+':
        cout << num1 + num2;
        break;
    case '-':
        cout << num1 - num2;
        break;
    case '*':
        cout << num1 * num2;
        break;
    case '/':
        cout << num1 / num2;
        break;

    default:
        cout << "Error! operator is not correct";
        break;
    }
    return 0;
}

提前致谢 enter image description here

【问题讨论】:

  • 它的行为与您编写的完全一样 - 提示并读取一个字符,然后提示并读取两个整数,然后仅检查 op 的值。 op读完后需要立即检查,在提示和读取后续整数值之前。

标签: c++ computer-science


【解决方案1】:

听起来您想要清除输入?

如果您希望某些输入被拒绝,那么在输入之后,您需要清除标准输入。

cin.clear();
cin.ignore(1000, '\n');

完成此操作后不要忘记再次请求输入,否则不会发生任何事情。 考虑把它放在一个while循环中并循环:

  • 询问接线员
  • 如果是接线员,请询问号码
  • 否则清除输入并再次请求操作员

希望这会有所帮助!

【讨论】:

  • 另外,if (!(cin &gt;&gt; op)) 会告诉你是否需要清理输出。
  • 太糟糕了,这不能回答所问的问题。 OP 询问为什么在输入错误字符和读取整数值之前没有发出错误消息。
  • 这不是 OP 寻找的答案
【解决方案2】:

你把cin输入操作放错地方了。如果您希望您的代码在输入错误的运算符后立即输出错误,则必须在检查运算符后移动cin部分,或者遇到无法识别的运算符时提前退出程序。

#include <iostream>
using namespace std;

void askNum(float &num1, float &num2)  //passing the values by reference 
{
    cout << "Enter the first operand: ";
    cin >> num1;
    cout << "Enter the second operand: ";
    cin >> num2;
}

int main()
{
    char op;
    float num1, num2;
    cout << "Enter operator either + or - or * or /: ";
    cin >> op;

    switch (op)
    {
        case '+':
            askNum(num1, num2);
            cout << num1 + num2;
            break;
        case '-':
            askNum(num1, num2);
            cout << num1 - num2;
            break;
        case '*':
            askNum(num1, num2);
            cout << num1 * num2;
            break;
        case '/':
            askNum(num1, num2);
            cout << num1 / num2;
            break;

        default:
            cout << "Error! operator is not correct";
            break;
    }

    return 0;
}

或使用您的代码,但像这样添加return 0(它有效,但我不推荐这样做)

default:
    cout << "Error! operator is not correct";
    return 0;

【讨论】:

    【解决方案3】:

    也许你想试试这个?

    #include <stdio.h>
    #include <iostream>
    #include <unordered_map>
    #include <functional>
    using namespace std;
    
    unordered_map < char,function < float (float, float) >>
      op_mapper{
      {'+', std::plus < float >()},
      {'-', std::minus < float >()},
      {'*', std::multiplies < float >()},
      {'/', std::divides < float >()}};
    int main ()
    {
      char op;
      float num1,num2;
      while (op != 'q') {
          cout << "Enter operator either + or - or * or /: ";
          cin >> op;
    
          if (op_mapper.find (op) != op_mapper.end ()) {
          cout << "Enter the first operand: ";
          cin >> num1;
          cout << "Enter the second operand: ";
          cin >> num2;
          cout << op_mapper[op] (num1, num2) << endl;
        } else {
          cout << "Error! operator is not correct";
        }
      }
    
      return 0;
    }
    

    【讨论】:

      【解决方案4】:

      读完运算符后,你需要检查运算符是否正确。下面的代码解决了你的问题。

      代码

      #include <iostream>
      using namespace std;
      int main()
      {
          char op;
          float num1, num2;
          cout << "Enter operator either + or - or * or /: ";
          cin >> op;
      
          if(op == '+' || op == '-'|| op == '*'|| op == '/'){
              cout << "Enter the first operand: ";
              cin >> num1;
              cout << "Enter the second operand: ";
              cin >> num2;    
      
          }else{
              cout << "Error! operator is not correct";
              return 0 ;
          }
      
          switch (op)
          {
              case '+':
                  cout << num1 + num2;
                  break;
              case '-':
                  cout << num1 - num2;
                  break;
              case '*':
                  cout << num1 * num2;
                  break;
              case '/':
                  cout << num1 / num2;
                  break;
          }    
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2015-09-28
        相关资源
        最近更新 更多