【问题标题】:C++ I can't compare char variable to values [duplicate]C ++我无法将char变量与值进行比较[重复]
【发布时间】:2015-03-23 01:17:20
【问题描述】:

必须为我要参加的第一门 cs 课程做这个。这是一个基本的计算器,它接受一个运算符和一个值并计算总和(从 0 开始的总和)。

#include <iostream>

using namespace std;

int main()
{
    char oprtr;
    float value, total = 0.0;

    cin >> oprtr >> value;

    while (oprtr != "q")
    {
        if (oprtr == "+")
            total += value;
        else if (oprtr == "-")
            total -= value;
    }
}

它还没有完成,但已经有问题了。它给出了一些错误,类似于“禁止将 char 值与 int 值进行比较”

【问题讨论】:

    标签: c++ char comparison


    【解决方案1】:

    双引号 ("q") 用于字符串。单引号 ('q') 用于字符。

    所以:

    while (oprtr != 'q')
    {
        if (oprtr == '+')
            total += value;
        else if (oprtr == '-')
            total -= value;
    }
    

    【讨论】:

      【解决方案2】:

      Char 表示Character,您必须使用单引号'' 表示这些,双引号"" 表示字符串。

      您收到此错误的原因是因为您尝试将字符与字符串文字(const char)进行比较,您得到的确切错误将是:

      操作数类型不兼容(“char”和“const char”)。

      以下代码将修复此错误:

      #include <iostream>
      
      using namespace std;
      
      int main()
      {
          char oprtr;
          float value, total = 0.0;
      
          cin >> oprtr >> value;
      
          while (oprtr != 'q')
          {
              if (oprtr == '+')
                  total += value;
              else if (oprtr == '-')
                  total -= value;
          }
      }
      

      【讨论】:

        【解决方案3】:

        比较字符串长度是 C 编程中的常用功能,因为它可以让您查看哪个字符串包含更多字符。这对于排序数据非常有用。比较字符串需要一个特殊的函数;不要使用 != 或 ==。

        http://www.techonthenet.com/c_language/standard_library_functions/string_h/strcmp.php

        【讨论】:

        • 这不是问题的答案,请在回答之前做你的研究。
        【解决方案4】:

        而且,由于您正在阅读语句或表达式一次,因此您无需在字符不等于 'q' 时进行循环。您基本上应该执行一项操作。此外,switch 是一个非常有用的结构,用于比较文字而不是几个 if。所以我将其简化为。

        #include <iostream>
        
        using namespace std;
        
        int main(){
            char op;
            float value, total = 0.0;
        
            cin >> op >> value;
            //it is important at this stage to check for errors, as they are most likely
            //to occur.
           if(!cin){
              cerr << "Error: format unknown! \n"; //basic error handled here, the prog outputs the error
           }
        
            //now perform the calculation
            switch(op){
               case '+': total += value;
                break;
               case '-' : total -= value;
                break; 
               case 'q' : break;        //i presume q is your quit character
               default: /*unknown character*/ cerr << "Unknown operation! \n";
           }
        
          cout << "Total: "<<total << endl;
        
          return 0;
        }
        

        这基本上读入一个表达式并将其添加到总数中。您可以修改它以读取任意时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-03
          • 2021-12-28
          • 2013-12-21
          • 1970-01-01
          • 2022-12-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多