登录 | 首页 -> 华新鲜事 -> 求学狮城 | 切换到:传统版 / sForum | 树形列表
问一个关于c++ input 的问题。。
<<始页  [1]  末页>> 

问一个关于c++ input 的问题。。problem:
input three lines: first line has a number
second line have two numbers
third line has a string of characters( the number of characters is not more than 1000)

my code:


# include<iostream.h>
int main(void){
int a;
cin>>a;
cout<<a<<"\n";

int x, y;
cin>>x>>y;
cout<<x<<" "<<y<<"\n";

char str[1001];
cin.getline(str, 1000);
cout<<str[2]; /*I put this line here to check whether the third line of input is read*/

return 0;
}

sample input and output:
first line input: 1
output: 1 // this is correct
second line input: 2 1
output: 2 1// this is also correc
but after the output 2 1 is displayed in the screen, the program suddenly end after prompting two blank lines..


the whole process is like this:
@sf3:~/c_programming[591]$ ./a.out
1
1
2 1
2 1

@sf3:~/c_programming[592]$



what is wrong with my code?
can anyone enlighten me?



[我行故我 (5-12 14:31, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

抛砖引玉我先去掉了前面两个正常运行的fragment,单独测试最后一个,程序正常。因此怀疑是standard in部分的问题。原因是上面输入1,2的以后有个enter,这个'\n'被存在了standard input stream里面,因此getline首先search到的是这个enter,也就是说end of line,自然不会继续读取input了。
为了验证这个猜想,我把getline的parameter改成了(str,100,' '),也就是说用space来结束,程序正常。
遗憾的是用c里面的fflush(stdin)没有用,不知道是不是VC++的问题,那位在unix上面试试看?
另外,我改成cin>>str,结果正常。

有不对之处,盼指正啊!
[kikicoco (5-12 18:10, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 kikicoco:抛砖引玉我先去掉了前面两个正常运行的fragment,单独测试最后一个,程序正常。因此怀疑是standard in部分的问题。原因是上面输入1,2的以...)Yes, it is caused by the return character.To ignore the return character, you can add the following line in you code before input.

cin.ignore();

This line is to remove the return character from the std input stream.

I happened to have tried this today.

Any comment is welcome.
[魅力十足 (5-12 18:25, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

(引用 魅力十足:Yes, it is caused by the return character.To ignore the return character, you can add the following line in you code before inpu...)why fflush(stdio) cannot work??[kikicoco (5-12 18:27, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

(引用 kikicoco:why fflush(stdio) cannot work??)Hehe,fflush(stdio) is a C style function. It is not suitable for C++.

:)
[魅力十足 (5-12 19:03, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 魅力十足:Hehe,fflush(stdio) is a C style function. It is not suitable for C++. :))还以为能兼容...原来如此阿
明天就要考C++了...
[kikicoco (5-12 19:06, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 魅力十足:Hehe,fflush(stdio) is a C style function. It is not suitable for C++. :))No, your explanation is wrong.Yes, fflush(stdin) does not work.

No, it doesn't work even in C!

The reason is that fflush really does flushing only on output streams, and it ignores input stream. So even in C, you have to manually read input stream in order to flush it.
[Flying (5-13 14:55, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼

多谢kikicoco 和 魅力十足[我行故我 (5-13 17:41, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 求学狮城 | [刷新本页] | 切换到:传统版 / sForum