首页 > 解决方案 > 无法在 c++14 中比较点和整数

问题描述

我正在尝试编写代码解码器。但是对于 if 语句中的所有比较,我得到以下错误:

'错误:ISO C++ 禁止指针和整数之间的比较 [-fpermissive]'

输入字符串的示例是".-.--""-..-.--"

#include <iostream>

#include <string>

using namespace std;
int main() {
  string s;
  int c[100], t = 0, l, i = 0;
  l = s.length();
  cin >> s;
  if (s[0] == '.') {
    c[0] = 0;
    t += 1;
    while (i < l) {
      if (s[i] == '-' && s[i + 1] == '.') {
        c[t] = 1;
        t += 1;
        i += 2;
      }
      if (s[i] == '.') {
        c[t] = 0;
        t += 1;
        i++;
      }
      if (s[i] == '-' && s[i + 1] == '-') {
        c[t] = 2;
        t += 1;
        i += 2;
      }
    }
  }
  if (s[0] == '-' && s[1] == '.') {
    c[0] = 1;
    t += 1;
    while (i < l) {
      if (s[i] == '-' && s[i + 1] == '.'
        '){
        c[t] = 1; t += 1; i += 2;
      }
      if (s[i] == '.') {
        c[t] = 0;
        t += 1;
        i++;
      }
      if (s[i] == '-' && s[i + 1] == '-') {
        c[t] = 2;
        t += 1;
        i += 2;
      }
    }
  }
  if (s[0] == '-' && s[1] == '-') {
    c[0] = 2;
    t += 1;
    while (i < l) {
      if (s[i] == '-' && s[i + 1] == '.') {
        c[t] = 1;
        t += 1;
        i += 2;
      }
      if (s[i] == ".") {
        c[t] = 0;
        t += 1;
        i++;
      }
      if (s[i] == "-" && s[i + 1] == "-") {
        c[t] = 2;
        t += 1;
        i += 2;
      }
    }
  }
  for (i = 0; i < t; i++) {
    cout << s[t];
  }
  return 0;
}

我该如何解决这个问题?

标签: c++14

解决方案


在您到达这里之前,您一直在使用单引号:

if(s[i]=="-"&&s[i+1]=="-"){

您需要将其更改为单引号,以便进行 int 到 int 的比较。

if(s[i]=='-'&&s[i+1]=='-'){

当你说

"-" 

您正在创建一个指针。当你说

'='

您正在创建一个 int。


推荐阅读