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

标签 c++ c++11 visual-c++ c++14

输入是: 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/

相关文章:

c++ - 为什么 C++ 不能推断模板类型?

c++ - SLEEP: (Sleep or usleep) 不是在 Linux 中挂起我线程中的所有内容,而是在 Windows 中?为什么?

c++ - error_code 与 errno

C++ 模板可变但静态

C++0x decltype 推导成员变量常量失败

c++ - 为什么 CLS() 在 C++11 中有不同的含义

c++ - boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error>>

c++ - 引用 null 可以吗?

c++ - 带有 "endl"的模板函数的 VS2015 编译错误

c++ - 如何找到 vector 的统计信息?最大值/最小值、众数、中值、平均值