popescubianca
popescubianca

Reputation: 51

Implicit conversion from char* to bool

I managed to solve my problem, it working properly and giving the correct results. The problem now is that I have this warning: Implicit conversion from char* to bool[readability-implicit-bool-conversion].

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

bool is_letter(const char s) {
  return ('a' <= s && s <= 'z') || ('A' <= s && s <= 'Z');
}

int main() {
  const int MAX_LENGTH = 260;
  const int VOWELS = 11;
  char is_vowel[VOWELS] = "aeiouAEIOU", s[MAX_LENGTH];
  ifstream fin("date.in");
  int k;
  cin >> k;
  int start = -1,nrVowels = 0, finish = 0, counter = 0;
  while (!fin.eof()) {
    fin.getline(s, MAX_LENGTH);
    int n = strlen(s);
    int have_word = 0;
    for (int i = 0; i < n; ++i) {
      if (is_letter(s[i])) {
        have_word = 1;
        if (strchr(is_vowel, s[i])) {
          ++nrVowels;
        }
        if (counter == 0) {
          start = i;
          finish = i;
          ++counter;
        } else {
          finish = i;
        }
      } else if (have_word == 1) {
        if (nrVowels >= k) {
          for (int i = start; i <= finish; ++i) {
            cout << s[i];
          }
          cout << "\n";
        }
        counter = 0;
        have_word = 0;
        nrVowels = 0;
      }
    }
    if (have_word == 1) {
      if (nrVowels >= k) {
        for (int i = start; i <= finish; ++i) {
          cout << s[i];
        }
        cout << "\n";
      }
      counter = 0;
      nrVowels = 0;
      finish = 0;
    }
  }
  return 0;
}

The error appears on the line where I am searching for the vowels "

        if (strchr(is_vowel, s[i])) 
 

"

Upvotes: 1

Views: 561

Answers (1)

Joseph Larson
Joseph Larson

Reputation: 9058

strchr() returns a char *. You're then using it in a boolean operation. While it works, the compiler is suggesting you change the code to:

if (strchr(...) != nullptr)

Then there is no implicit conversion.

Note that there are people who think C++ implicit conversion should be removed. Jason Turner has a talk on this on YouTube. I have no idea how many bugs I've had over the years due to implicit conversion, which is why your compiler warns you about it.

Upvotes: 2

Related Questions