Unable to compare pointes and integeres in c++14

Viewed 45

I'm trying to program a code decoder. But I get the following error for all the comparisons in the if statements:

'error: ISO C++ forbids comparison between pointer and integer [-fpermissive]'

The examples for the input string are ".-.--" and "-..-.--".

#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;
}

How do I resolve this issue?

2 Answers

You were using single quotes until you got here:

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

You need to change it to single quotes so you have an int to int comparison.

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

When you say

"-" 

you are creating a pointer. When you say

'='

you are creating an int.

(" ") is a string literal which is char const * which is a pointer and (' ') is char which get promoted to int, so you can't compare them. They must be type compatible.

Related