gpt4 book ai didi

c++ - 二维数组在退出循环后移除其元素

转载 作者:行者123 更新时间:2023-11-30 03:22:28 24 4
gpt4 key购买 nike

输入是: Hello World
下面的程序应该用words[0]记录“hello”,用words[1]记录“world”

int rowIndex= 0;
char words [100][100];
for (int x = 0 ; x < input.length(); x++)
{
if (input[x]>='a'&&input[x]<='z' || input[x]>='A'&&input[x]<='Z')
// This condition is to avoid recording spaces in the array
{
words[rowIndex][x]= input[x];
cout << words[rowIndex][x]<<" ";
}
else {
rowIndex++;
// Once it finds a space, it records the following characters into the next index
cout << " Index: " << rowIndex <<endl;
}
}

输出:
h e l l o
索引:1
w o r l d

cout <<"Index 0: "<< words[0] <<endl;

输出:你好

cout <<"Index 1: "<< words[1] <<endl;

输出:“不输出任何东西”(为什么不输出“world”)
*************************************************** *******
为什么数组不保存words[1]中的字符而只保存words[0]中的字符
注意:我尝试用动态二维数组来做,但同样的问题发生了。

最佳答案

cout <<"Index 1: "<< words[1]通过访问 words[1][0] 表现出未定义的行为那从未被初始化。

关于c++ - 二维数组在退出循环后移除其元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51109802/

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