0

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])) 
 

"

  • 6
    Just write `if (strchr(is_vowel, s[i]) != nullptr)` That's the test that is happening implicitly, just make it explicit. – john Dec 07 '22 at 11:22
  • 1
    It is just a warning that *some* conversions from `char*`are wrong, like `if ("Hello")` will always be true. You can make the compiler calm down by `if (strchr(is_vowel, s[i]) != nullptr)`. – BoP Dec 07 '22 at 11:23
  • 1
    Your indexing is weird, `start=-1, i=start, s[i]` is not what you want. Same for `<=finish`. Also the obligatory [why is `while(!in.eof())` wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Quimby Dec 07 '22 at 11:23
  • what this code suppose to do? Note it is written to much like C code. – Marek R Dec 07 '22 at 11:25

1 Answers1

1

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.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36