数组越界,找不到错误

img

img


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char a[100],b[100];
    int i = 0,len,k=0;
    while ((a[i] = getchar()) != '\n')
    {
        i++;
    }
    a[i + 1] = '\0';
    len = i;
    for (i = 0; i < len; i++)
    {
        if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' ||
            a[i] == 'u' || a[i] == 'o')
        {
            b[k++] = a[i];
        }
    }
    b[k + 1] = '\0';
    for (i = 0; i < k; i++)
    {
        cout << b[i];
    }
    return 0;
}

第 13 行改为a[i] = 0;
第23行改为b[k] =0;
另外第9行要保证输入字符个数小于100个,不能等于100

按照你写的这个,只要输入字符数不超过98个就行,否则就会溢出。如果需要输入的更多,可以用string、动态数组、还有vector之类的容器。
还有就是,你这样写有点麻烦了,有些没用的代码可以直接省略,这样看的也会清楚一些, 我用string写了一个。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(void)
{
    string temp= {};
    cin >> temp;
    for (int i = 0; i < temp.size(); i++)
    {
        if (temp[i] == 'a' || temp[i] == 'e' || temp[i] == 'i' || temp[i] == 'o' || temp[i] == 'u')
        {
            cout << temp[i];
        }
    }    

    return 0;
}