如何解决如何检查C++中输入行中是否有空格
我正在尝试检查输入行中是否有空格。
我已为输入编入索引,但出现错误:“预期的 ';'预期')'"
string mike20;
int count = 0;
while (getline(cin,mike20))
for (mike20[0]); (isspace(mike20[0]); ++mike20[0])
return 0;
解决方法
首先,它们是您代码中的语法错误,更像是 for (mike20[0]; isspace(mike20[0]); ++mike20[0])
。其次,你似乎不明白 for 循环是如何工作的。由于没有任何变化,因此循环是无限的。
您需要一个变量来浏览字符串的每个字符并对每个字符进行测试。
int cursor;
for (cursor = 0; cursor < number_of_char_in_mike20; ++cursor)
{
isspace(mike20[i]);
}
此代码不完整,您需要声明并初始化 number_of_char_in_mike20
变量,否则必须定义 isspace() 函数。
PS:不要忘记用大括号 {} for while 循环和 for 循环,不要只重复第一条指令。
for (initialisation(only once); test(each time); increment(each time))
first_instruction; // each time
second_instruction; // after the loop end
third_instruction; // after the loop end
fourth_instruction; // after the loop end
fifth_instruction; // after the loop end
for (initialisation(only once); test(each time); increment(each time))
{
first_instruction; // each time
second_instruction; // each time
third_instruction; // each time
}
fourth_instruction; // after the loop end
fifth_instruction; // after the loop end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。