gpt4 book ai didi

c++ - for循环后的C++代码无法执行

转载 作者:行者123 更新时间:2023-12-03 07:21:59 25 4
gpt4 key购买 nike

我有点奇怪,for循环后的代码没有执行,但是编译器没有发出任何警告:

#include <iostream>
#include <iterator>

bool isVowel(char ch)
{
char vowels[]{"aeiouAEIOU"};

char *p = std::find(std::begin(vowels), std::end(vowels), ch);
if (p != std::end(vowels))
{
std::cout << ch << " is a vowel" << '\n';
return true;
}
else
{
std::cout << ch << " is not a vowel" << '\n';
return false;
}
}

int main()
{
char name[]{"MollieU"};
int numVowels{0};
std::cout << name << '\n';
int length{std::size(name)};
for (char* ptr{name}; ptr < (name + length); ++ptr)
{
if (isVowel(*ptr))
++numVowels;
}

std::cout << name << " has " << numVowels << " vowels." << '\n';

return 0;
}
它具有如下输出:
MollieU
M is not a vowel
o is a vowel
l is not a vowel
l is not a vowel
i is a vowel
e is a vowel
U is a vowel
但是 std::cout << name << " has " << numVowels << " vowels." << '\n';永远不会运行。我在循环后输入的任何代码都永远不会运行。我不确定 return 0是否运行,但是文件编译成功。我在 g++ -std=c++2a上使用 osx

最佳答案

尝试使用以下内容:

int length { std::size(name) - 1 };

关于c++ - for循环后的C++代码无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64819837/

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