gpt4 book ai didi

c++ - 如何修复 "Variable declaration in condition must have an initializer"

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:09:11 27 4
gpt4 key购买 nike

我正在编写一个程序来计算用户输入的元音数量,但它给我错误“条件中的变量声明必须有一个初始值设定项”。你如何修复它?

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

最佳答案

使用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();
}

关于c++ - 如何修复 "Variable declaration in condition must have an initializer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com