I have a C++ program that calls for user input (integer) at two different points in the program (cin >> variable1, cin >> variable2). The first input is successful. The program then "sails" right past the second request without waiting for the user input.
I've checked the state of cin, I've used cin.clear() and cin.ignore(), after the first input. Nothing that I have tried will make it pause for the second input.
/* 9/13/10 main for test_input
* Test successive calls to cin >>. cin >> int1; cin >> int2.
*/
#include <iostream>
#include <vector>
using namespace std;
#define MAXCODONS 1138
int main (int argc, char * const argv[]) {
vector<int> col_nums; // col nums collected from stdin
// prompt for input from stdin (keyboard)
cout << "enter column number(s) to be displayed followed by Ctrl+D\n"
<< "(in range [0:MAXCODONS) / 3) 1138)\n";
int col=0;
while( cin >> col ) {
if( col < MAXCODONS && col > 0 )
col_nums.push_back(col);
Please use code tags and properly format that post. Also what is the input? Could you capture the data from a console window during an attempted run? I'm curious as to what you are doing. I do not know why you are using the if(cin.eof()) check. Are you entering in a whole bunch of numbers and then pressing enter so that the while loop reads all of the data?
I would read the entire I/O section on this website, especially the part on error and range checking. Then double check your program and make the necessary improvements as recommended. Let us know if any of that info helps. http://www.parashift.com/c++-faq-lite/input-output.html
It is difficult to write this kind of console app while considering all of the possible error and exit conditions. I would still recommend reading those FAQs and rethinking the design a bit. How do you know that duplicate column numbers weren't entered? What if you fat finger a key and enter a nonnumeric number? What does the program do then?
I agree that this is not the most sophisticated design. (I am hardly a power programmer).
Some small measure of progress. I reversed the order of the calls asking for input. The single int input to "cutoff" comes first. The call for input to "col" is now second. Each int to col is followed by ENTER and the input is terminated by CONTROL+D. This seems to work, but is hardly a solution. Is the original problem caused by CONTROL+D? If so, why doesn't the cin.ignore() work?
I did look at the FAQ and learned a lot.
Thanks for you help,
Jordon
P.S.
I wrote this code after studying a section on I/O in Stroustrup's Programming: Principles and Practice Using C++.