编译器在 strstok() 之后不继续 (C++)
Compiler doesn't proceed after strstok() (C++)
我使用了带有while循环的strtok()
函数并对其进行了编译,但while循环之后的ANYTHING似乎对编译器不存在:
int main()
{
int j = 1;
char a[60] = "ion ana adonia doina doinn ddan ddao . some other words ";
char *pch[36];
pch[0] = strtok(a, ".");
while (pch[j + 1] != NULL)
{
cout << pch[j - 1] << endl;
pch[j] = strtok(NULL, ".");
j++;
}
cout << "hello!"; //this doesnt display at all
return 0;
}
我正在使用c-free。
除其他外,while循环结束条件是错误的。您正在检查pch[j + 1]
,它总是未初始化的内存,导致循环不可预测地继续,直到您在内存中遇到零,这可能会导致循环停止。
另一方面,我强烈反对在C++中使用strtok
,因为它会破坏字符串。Boost有一个非常好的字符串解析功能,即使在基本的C++语言中,它也足够简单,可以使用内置的字符串功能进行大多数解析。
您的代码很奇怪。试试这个:
int main()
{
int j = 0;
char a[60] = "ion ana adonia doina doinn ddan ddao . some other words ";
char *pch[36];
pch[j] = strtok(a, ".");
while (pch[j] != NULL)
{
cout << pch[j] << endl;
pch[++j] = strtok(NULL, ".");
}
cout << "hello!"; //this doesnt display at all
return 0;
}
相关文章:
- 循环后如何继续阅读
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- C++:继续创建新的变量可以吗
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 循环不继续无法弄清楚为什么
- 附加到 gdb 会中断并且不会继续该过程
- C++ 在信号处理程序后继续执行
- 为什么我的Qt程序在断言失败后继续运行?
- 如何打印下一个字母直到 Z 并继续到 A?
- 超时后,线程睡眠不会继续执行
- 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?
- 如何创建一个 while 循环,继续循环,直到在C++中输入'using streams'键?
- 失败后可以继续从流读取吗?
- 在构造函数中删除后继续执行
- 在程序继续C++时保持进程运行
- 继续运行程序而无需任何干预,直到要求退出为止
- while 循环 C++ 结束,但不继续到其余语句
- C++继续获取表达式必须具有类类型