1

I am writing a program that counts the number of vowels that the user inputs but it is giving me the error "Variable declaration in condition must have an initializer". How do you fix it?

#include <iostream>
using namespace std;

int isVowel(char c) 
{
  char Vowels[] = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
  if (c in Vowels)
    return true;
}

int main()
{
  int n;
  int numVowel = 0;
  char c;

  cin >> n;

  for (int i = 0; i < n; ++i)
  {
    cin >> c;
    if (isVowel(c))
      numVowel++;
  }

  cout << "Number of vowels = " << numVowel << endl;

  return 0;
}

5 Answers5

8

Use std::find

#include <algorithm>
#include <array>

bool isVowel(char c)
{
    static constexpr std::array<char, 10> Vowels{ 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };
    return std::find(Vowels.begin(), Vowels.end(), c) != Vowels.end();
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
john
  • 85,011
  • 4
  • 57
  • 81
3

Others have answered adequately to this question, but the following could be a valid alternative:

bool isVowel(const char c) {
  switch (tolower(c)){
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
          return true;
  }
  return false;
}

I ran some benchmarks and comparing all solution proposed on a random string of size 10k ; Here are the results (the lower the bar the better=faster the code):

  1. Clang 7.0 -O3 -std=c++20

enter image description here

  1. gcc-8.1 -O3 -std+c++20 enter image description here
Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
  • Using default in the switch to return false; rather than returning false at the end? – TuanDT Mar 28 '19 at 09:57
  • Might also work. I personally find this way more clear (and shorter) – Davide Spataro Mar 28 '19 at 10:00
  • Yes, it does work, but the reason I prefer putting a default is that you signal that the function will end in the switch. It blocks other people from putting logic between the switch and the return false. But it's only my personal take. I think your way works well too. I'm just being really picky here. – TuanDT Mar 28 '19 at 10:05
  • @TuanDT yeah, it make sense. – Davide Spataro Mar 28 '19 at 10:28
2

There is no in operator in C++.

You probably want this:

int isVowel(char c)
{
  static const char Vowels[] = { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };
  for (const auto & v : Vowels)
    if (c == v)
      return true;

  return false;
}

Bonus: you should make Vowels static and const, it might be a bit more performant.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • What does "auto & v : Vowels" mean? –  Mar 28 '19 at 09:39
  • 2
    Time to open your C text book: https://en.cppreference.com/w/cpp/language/range-for – Jabberwocky Mar 28 '19 at 09:41
  • Same as `for x in Vowels` in Python, near enough (except & is a ref so will let you change things even chars, unless you use `const`) – doctorlove Mar 28 '19 at 09:42
  • You can save some space if you only store lowercase vowels and use `to_lower` when performing the comparison. Moreover, you can pass `c` as `const`. – Davide Spataro Mar 28 '19 at 09:45
  • @DavideSpataro for const: already edited, but calling `to_lower` take probably more than the 5 bytes you save storing only lowercase vowels. – Jabberwocky Mar 28 '19 at 09:47
  • In my opinion, which might be wrong, I think the code as above is good enough. I think optimization should be done using a profiler when the need arises. You don't want to piss off the legendary Donald Knuth by prematurely optimizing :) – TuanDT Mar 28 '19 at 09:51
1

I think the easiest is this one:

bool isVowel(char c) 
{
  char Vowels[] = "AEIOUaeiou";
  if ( strchr(Vowels,c) )
    return true;
  else
    return false;
}
Axel Podehl
  • 4,034
  • 29
  • 41
0

To this, you can also replace if in with for-loop in following way

for(int i=0 ; i < Vowels.length-1 ; i++) {
     if(Vowels[i]==c)
     return true; 
}
r_batra
  • 400
  • 3
  • 14