-
Cin.get()
I have one question using cin.get(). I have posted the code below. The problem is, when I run the program it terminates right away, and doesnt wait for the user to input the information. Why?
Code:
void get_info()
{
cout << "Enter the blocker hand (L or R)";
cin.get(blocker_hand, HAND);
cout << endl;
cout << "Enter the glove hand (L or R)";
cin.get(glove_hand, HAND);
}
// declerations
private:
char glove_hand[HAND];
char blocker_hand[HAND];
and HAND is a constant of length 1.
There is more code to the actual program, but this is the only part causing the problem.
Thanks
-
There are two problems. First, the minimum length for a string is 2 chars, period. That accomadates a single char and the null-terminating zero. Second, cin.get() does not flush the newline character from stdin. Try instead:
Code:
const int HAND = 2;
char blocker_hand[2];
char glove_hand[2];
//....
cout << "Enter the blocker hand (L or R)";
cin.get(blocker_hand, HAND);
cout << endl;
fflush(stdin);
cout << "Enter the glove hand (L or R)";
cin.get(glove_hand, HAND);
//...
Cheers.
-
Thanks, I forgot obout the terminating character. It still, however, does not let me get the second input.
-
Did you forget to use fflush()? It should be placed between the two inputs as my post described. If all else fails, use the time - honored fgets()!
-
i've read several knowledgeable sources that recommend avoiding use of C style buffer handling (fflush) and C++ style buffer handling (get()) in the same program. My advice is to use getline() in place of get(). getline() will remove the terminating char from the buffer, whereas get() and >> will not. Alternatively, use ignore() with reasonable parameters.
-
-
Code:
const int HAND = 2;
char blocker_hand[2];
char glove_hand[2];
//....
cout << "Enter the blocker hand (L or R)";
cin.get(blocker_hand, HAND);
cout << endl;
fflush(stdin);
cout << "Enter the glove hand (L or R)";
cin.get(glove_hand, HAND);
//...
You've been here long enough to know better than that Sebastiani. ;) fflush ( stdin ) is undefined and there are better ways to do this in C++. Look into cin.ignore() for a good way to discard extraneous characters in the input stream.
-Prelude
-
Contrary to what everyone seems to say about fflush(), it works fine on my compilers, though again, I never use fflush() since I am comfortable with fgets() and hence never need it, (nor cin or scanf() for that matter). But you are right. No sense in teaching others bad habits :)
-
>Contrary to what everyone seems to say about fflush(), it works fine on my compilers
It works peachy on mine as well, but I'd rather put my trust in the C++ standard than in Microsoft.
-Prelude
-
The problem is that Microsoft actually have examples in the VC++ help using fflush(stdin). But then again, they also have "void main()"..
-
Re: Cin.get()
Quote:
Originally posted by MethodMan
I have one question using cin.get(). I have posted the code below. The problem is, when I run the program it terminates right away, and doesnt wait for the user to input the information. Why?
Code:
void get_info()
{
cout << "Enter the blocker hand (L or R)";
cin.get(blocker_hand, HAND);
cout << endl;
cout << "Enter the glove hand (L or R)";
cin.get(glove_hand, HAND);
}
// declerations
private:
char glove_hand[HAND];
char blocker_hand[HAND];
and HAND is a constant of length 1.
There is more code to the actual program, but this is the only part causing the problem.
Thanks
Not too sure because I have not used this method, but if you just want character input than:
char a, b;
cin >> a;
cin >> b;