how would i go on about ending the input in the while loop in this program? for example if i input 1 2 3 4 5 and press enter, nothing happens and the program doesn't continue.
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. 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. 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. See reference https://stackoverflow.com/questions/4563617
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.