No, the issue is how you have pre-loaded the input.
A step-through will help:
1.
cout << "Enter a number> "; cin >> number_one;
2. The user types
and presses ENTER.
3. That sequence of keypresses gets sent to the program as "
37\n". (The ENTER key was translated into a newline character.)
4. The
cin statement skips all leading whitespace (there isn't any), reads "
37", translates it into the number 37, and sticks that in the
number_one variable.
5. Notice that the "
\n" is still waiting to be read by the program.
6.
cout << "Enter another> "; cin >> number_two;
7. Again, the user types something like
and presses ENTER.
8. That sequence gets sent to the program as "
-2\n".
9. At this point, the list of characters waiting to be read by the program is "
\n-7\n".
10. The
cin statement skips all leading whitespace (reading and ignoring that first "
\n", leaving "
-7\n"), reads "
-7", converts it into the number -7, and assigns that value to
number_two.
11. Notice how there is yet again a lone "
\n" waiting to be read by the program.
12.
cout << "Press ENTER to continue..."; cin.ignore( ..., '\n' );
13. The
cin statement now ignores everything up-to and including the next "
\n".
14. But wait! There is a leftover "
\n" sitting in the input buffer waiting to be read. So
cin reads it (leaving "" in the input buffer), and the program terminates before the user can get his fingers to move.
And there lies the caveat to using
cin >> anything
when dealing with the user. Remember:
The user always expects to have to press ENTER after every input
(in a streams application). Unless the program shows him otherwise by immediately responding to keypresses (in a CUI application).
So, you have to be careful when getting input.
Method one
Once you are done any sequence of "cin >>"s you should
cin.
ignore() that lone newline before continuing with the rest of the program. In your program, input occurrs on lines 22..25, so a good place to ignore() that unread newline is right after line 25.
Method two
Don't "cin >>". Use
getline() and
std::stringstream instead. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <ciso646>
#include <sstream>
#include <string>
template <typename ValueType>
std::istream& read( std::istream& ins, ValueType& value )
{
using namespace std;
string s;
getline( ins, s );
if (ins.good())
{
stringstream ss( s );
ss >> value;
if (!ss.good())
ins.setstate( ss.rdstate() );
}
return ins;
}
#include <iostream>
using namespace std;
int main()
{
float f;
integer i;
cout << "Quiz!\nWhat is the average airspeed of an unladen European Swallow (in m/s)? ";
read( cin, f );
if (!cin or (f < 10.5) or (f > 11.5)) // [1]
cout << "Aiiiieeeee!\n";
else
{
cout << "What is the day of the month today? ";
if (!read( cin, i ))
cout << "Well at least guess...\n";
else
cout << "Close enough. :-)\n";
}
return 0;
}
| |
[1]
http://answers.yahoo.com/question/index?qid=20070409040602AACDUk6
Hope this helps. :-]