为什么程序不能立即输出?
Why can't the program output immidiately?
当我输入"111 111"然后按enter键时,输出什么都不显示。然后,当我按enter键两次时,就会出现预期的输出。为什么?
#include<iostream>
using namespace std;
int main()
{
char seq[10];
//initialize the sequence
for (int i = 0; i<10; i++)
{
seq[i] = ' ';
}
//read characters from the keyboard
for (int i = 0; i<10; i++)
{
cin.get(seq[i]);
if (seq[i] == ' ')
{
break;
}
}
//the output should be the sequence of characters
//users typed before
cout << seq;
system("pause");
return 0;
}
您可以使用头文件string
,它提供了更大的灵活性,如下所示:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string seq;
//initialize the sequence
//read characters from the keyboard
getline(cin,seq);
//the output should be the sequence of characters
//users typed before
cout << seq;
system("pause");
return 0;
}
针对OP的问题更新:
在所描述的情况下,您从未从标准输入输入