问一个关于c++ input 的问题。。
所在版块:求学狮城 发贴时间:2003-05-12 14:31

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
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?



.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

Don't forget to always have a dream
我曾是孤单的飞鸟
飘荡在远方的天空
如今我已飞得太久
才知道我就是春天
我用力地挥动翅膀
开始寻找家的方向
我用力地挥动翅膀
溶进这宽阔的天空
 相关帖子 我要回复↙ ↗回到正文
问一个关于c++ input 的问题。。 我行故我   (939 bytes , 508reads )
多谢kikicoco 和 魅力十足 我行故我   (0 bytes , 176reads )
抛砖引玉 kikicoco   (495 bytes , 214reads )
Yes, it is caused by the return character. 魅力十足   (238 bytes , 273reads )
why fflush(stdio) cannot work?? kikicoco   (0 bytes , 239reads )
Hehe, 魅力十足   (68 bytes , 223reads )
No, your explanation is wrong. Flying   (241 bytes , 153reads )
还以为能兼容... kikicoco   (29 bytes , 165reads )