Succesive calls to cin fail

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.

Any help or suggestions?

Jordon Hirshon
(cin >> variable1, cin >> variable2).

That usage for cin is incorrect
Instead: cin>>a>>b;
My post did not accurately represent what the program is doing. cin >> variable1 and cin >> variable2 are separate calls.

Jordon Hirshon
Can you past your code here ?
Here's the code that exhibits the problem:

/* 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);

if( cin.eof() )
break;
}
cin.ignore();
cin.clear();
cout << "col_nums.size() = " << col_nums.size() << '\n';

// prompt for input from stdin (keyboard)
cout << "enter a value for \"cutoff\" (in range [1:23])\n";
int cutoff = 0;

while( cin >> cutoff ) {
if( cutoff >= 1 && cutoff <= 23 )
break;
cout << cutoff << "is not in the range [1:23]; try again\n";
}
cout << "cutoff = " << cutoff << '\n';
return 0;
}


I cannot see any code before the last line which will keep the window open.
Also try initializing cutoff with a value in the valid range.
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
kempofighter;

Thanks for your response.

For the first input I am entering several ints, and terminating the input with CONTROL+D. For the second input I'm entering a single int.

jordon Hirshon wrote:
For the first input I am entering several ints, and terminating the input with CONTROL+D.

Are you on windows? Try CONTROL+Z instead of CONTROL+D then.
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?
Kempofighter;

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++.
Topic archived. No new replies allowed.