In your original code, first you make the user
cin >> n;
You intend to have them to press enter here, but that makes
cin.peek() == '\n'
right from the start, invalidating your while loop.
To alleviate this, I changed it to a do-while loop, so that it will always ask for the set of numbers to input at least once, until the user presses enter
again.
std::vector::size_type is the proper way to get the type that vector::size() returns. It is necessary that both things being compared in std::min are the same type, so this limits any ambiguity there. In other words,
std::min(int, std::vector::size)
is not valid.
The reason I used std::min is the reason that helios spoke about. What if you set your first number (the size) to 5, but then input 4 elements, and then pressed enter? Your loop would try to go out of bounds. Therefore, the std::min is necessary to stay in bounds -- it returns the smaller number of the two (the actual size of the vector vs. the size you input at the beginning).
The premise of your code is quite strange. Instead of a while loop, I would just do a for loop to get n elements (you can set the size of the vector in advance), and then print those n elements. Regardless of how many times the user presses enter. And use proper prompts, like "Enter the size of your data". Much less confusing.
What do you mean by "a proper command window?" |
You dedicate three lines towards an ugly hack to get the console window to stay up so that you can see the output of your program. This... works, but depending on your IDE, you should be able to make the command prompt stay open after the program ends, or just run the program through an actual command-line window/terminal. If you do this, you won't have to worry about ugly hacks like that.