注册 登录
编程论坛 C++教室

到底是什么地方出了问题,求教

醉生梦死 发布于 2007-09-01 12:44, 642 次点击

我想输入一组数字,用字符串存储,并用pos记录输入空格的位置
#include <iostream>
using namespace std;

int main()
{
char c[50];

while (cin>>c)
{
int pos=0;
while (c[pos]!=' ')
{
pos++;
}
cout<<pos<<endl;
}
}

sample input:
1313215 153464 回车
结果输出的不是7,而是148,这是为什么?错误出现在哪里了?

6 回复
#2
雨中飞燕2007-09-01 12:56
cin>>c遇到空格就不再接收,除非你用getline



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#3
醉生梦死2007-09-01 12:59
回复:(雨中飞燕)cin>>c遇到空格就不再接收,...

唉!真笨,既然把这么重要的知识点忘了,谢谢啦

#4
hgxwd3102007-09-13 23:47
还是不懂啊!!请解释一下整个过程好吗?
#5
snakeImao2007-09-15 00:24

#include <iostream>
using namespace std;
int main()
{
char c[50];
int pos=0;
cin.getline(c,50);
while (c[pos]!=' ')
{
pos++;
}
cout<<pos<<endl;
return 0;
}

#6
lzwyelang2007-09-15 14:11
回复:(醉生梦死)到底是什么地方出了问题,求教

请问怎么理解while (cin>>c)呢,什么时候才跳出循环?????????

#7
tancui2007-09-15 14:20
空格用cin不能读入
1