[英]End the input in a while loop
how would i go on about ending the input in the while loop in this program? 我将如何继续在该程序的while循环中结束输入? for example if i input 1 2 3 4 5 and press enter, nothing happens and the program doesn't continue. 例如,如果我输入1 2 3 4 5并按Enter键,则什么也没有发生,并且程序无法继续。
Note: On the assignment it says that ctrl+D terminates the input but it's not working and i'm not sure if it should do that by itself or i have to write something in order for it work. 注意:在作业上,它说ctrl + D终止了输入,但是它不起作用,我不确定是否应该单独执行此操作,或者我必须写点东西才能使其正常工作。 Thanks! 谢谢!
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
int main ()
{
int smallest, second_Smallest,a;
vector<int> v;
cout << "Enter the numbers in random order: " << endl;
while (cin >> a)
{
v.push_back(a);
}
if(v[0]<v[1])
{
smallest = v[0];
second_Smallest = v[1];
}
else
{
smallest = v[1];
second_Smallest = v[0];
}
for(int i=1; i<v.size(); i++)
{
if(smallest>v[i])
{
second_Smallest = smallest;
smallest = v[i];
}
else if(v[i] < second_Smallest)
{
second_Smallest = v[i];
}
}
cout << second_Smallest;
return 0;
}
Your program works fine. 您的程序运行正常。 It simply closed the output windows too fast for you to see the output. 它只是太快地关闭了输出窗口,以至于您看不到输出。 Start running your application using CTRL+F5 instead of only F5. 使用CTRL + F5而不是仅F5开始运行应用程序。 This way the output window will stay open after termination of your program. 这样,程序终止后,输出窗口将保持打开状态。
The explanation for why CTRL-D terminate the while loop, is because stdin interpret the CTRL-D as an end-of-file. 为何CTRL-D终止while循环的原因是因为stdin将CTRL-D解释为文件结尾。 See reference https://stackoverflow.com/questions/4563617 请参阅参考https://stackoverflow.com/questions/4563617
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.