gpt4 book ai didi

c++ - 为什么我的程序在我向数组输入一定数量的字母后就结束了?

转载 作者:行者123 更新时间:2023-11-28 04:07:44 26 4
gpt4 key购买 nike

每次我向数组中插入一定数量的字母时,我的控制台都会立即关闭。我的代码有问题吗?另外,有没有不同的方法来计算人声的数量?

using namespace std;

int main()

{
int i=0;
int leng=1;
int voc = 0;
char seq[leng] = {};
cout << "lets create the sequence..." << endl << "enter '*' to end sequence" << endl;

for (i; i < leng; i++) {

leng++;
cin >> seq[i];
if (seq[i] == '*') {
break;
}
}

cout << "lets check the frequency of the 5 vocals..." << endl;

for (int z=0; z < leng; z++) {

if (seq[z] == 'a' || seq[z] == 'e' || seq[z] == 'i' || seq[z] == 'o' || seq[z] == 'u') {
voc+=1;
}
}

leng-=2;

cout << "so out of... " << leng << " letters, " << voc << " of them are vocals.";
}

最佳答案

我认为你的代码无法编译,原因如下:

  1. 不包括 I/O 头文件
  2. 不能使用变量作为数组长度
  3. 没有返回
  4. 等等

我按照你的方式重写了你的程序:

 #include <iostream>
using namespace std;

bool is_vacol(char letter)
{
if(letter == 'a' || letter == 'o' || letter == 'e' || letter == 'i' || letter == 'u')
return true;
else
return false;
}

int main()
{
int count = 0;
string letters;
cout << "Please enter the letters" << endl;
cin >> letters;
int len = letters.size();

cout << "lets check the frequency of the 5 vocals..." << endl;
for(int i = 0;i < len; i++)
{
if(is_vacol(letters[i]))
count++;
}
cout << "so out of... " << len << " letters, " << count << " of them are vocals.";
return 0;
}

关于c++ - 为什么我的程序在我向数组输入一定数量的字母后就结束了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410841/

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