如何解决“条件中的变量声明必须具有初始化程序”

Yol*_*Hui 1 c++ c++11

我正在编写一个计算用户输入的元音数量的程序,但它给我的错误是“条件中的变量声明必须具有初始化程序”。您如何解决?

#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;
}
Run Code Online (Sandbox Code Playgroud)

joh*_*ohn 8

使用 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();
}
Run Code Online (Sandbox Code Playgroud)