#include <iostream> /// cin, cout
usingnamespace std;
/// test the get(c) stream member function
void get0()
{
char c;
cout << "Please type some characters: ";
cin.get(c);
cout << "the 1st "
<< " character typed: "
<< c << endl << endl;
}
int main()
{
get0();
}
Please type some characters: the 1st character typed:
My observations (and queries) are as follows:
1) On examining this code, I feel it is working as it should (though not as intended by me). cin 's stream buffer is empty; therefore cin.get(c) returns a null string, which is then displayed. I think this is how the function is expected to work, right?
2) What I would like to do, however, is to use the unformatted I/P get() function to read cin, while waiting for the user to enter data. I feel this may not be possible using the unformatted I/P get() function with cin. Is this correct? Is there any way to do so?
If not, it would mean that the unformatted I/P get() functions can be used (to get the intended behavior noted above) only with input string streams and input file streams, but not with cin. Is this correct?
Online compilers and runners often provide empty input to the program being run if the user doesn't state what input to provide. You're better off running your code locally.
#include <iostream> /// cin, cout
usingnamespace std;
/// test the get(c) stream member function
void get0()
{
char c;
cout << "Please type some characters: ";
cin.get(c);
cout << "the 1st "
<< " character typed: "
<< c << endl << endl;
}
/// test the get(p, n) stream member function
void geta()
{
char word[10];
int n = 5;
cout << "Please type some characters: ";
cin.get(word, n);
cout << "the 1st " << n - 1
<< " characters typed: "
<< word << endl << endl;
}
int main()
{
get0();
geta();
}
Running this offline, get0() works, but not geta().
#include <iostream> /// cin, cout
#include <climits> /// INT_MAX
usingnamespace std;
/// test the get(c) stream member function
void get0()
{
char c;
cout << "Please type some characters: ";
cin.get(c);
cout << "the 1st "
<< " character typed: "
<< c << endl << endl;
}
/// test the get(p, n) stream member function
void get1()
{
char word[10];
int n = 5;
/// Skip the rest of the buffer.
cin.ignore(INT_MAX);
cout << "Please type some characters: " << endl;
cin.get(word, n);
cout << "the 1st " << n - 1
<< " characters typed: "
<< word << endl << endl;
}
int main()
{
get0();
get1();
}
However, there's still a problem, both online as well as offline:
1) In the 2nd function get1(), an output prompt should have appeared followed by the user being allowed to enter some chars. (That's the sequence in which these statements are specified.) Instead, in the local program, the program pauses for user input and on Ctrl/Z, displays the prompt message. Again, no output is displayed.
You've told it to clear INT_MAX from the buffer. So it will. You're going to have to put INT_MAX into it so it can clear them all. It's sitting there waiting for more input in the buffer to clear.
Alternatively, cin.ignore(INT_MAX, '\n') so it clears INT_MAX or until the next '\n' it encounters.
Instead, in the local program, the program pauses for user input and on Ctrl/Z, displays the prompt message.
Your use of ignore effectively tells cin to ignore all input - hence you found yourself in the awkward situation of having to break out of that state by signalling end of stream. Once the cin end is reached, no further input is possible. (you might find some workaround but I'm not sure how reliable that would be).
The ignore() function properly belongs inside function get0() because that is where the user typed some characters ending with a newline. Consequently that is where you need to ignore until the newline is reached and discarded.
#include <iostream>
#include <limits>
usingnamespace std;
void ignore_until_newline()
{
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
/// test the get(c) stream member function
void get0()
{
char c;
cout << "Please type some characters: ";
cin.get(c);
// Skip the rest of the buffer.
ignore_until_newline();
cout << "the 1st "
<< " character typed: "
<< c << endl << endl;
}
/// test the get(p, n) stream member function
void get1()
{
char word[10];
int n = 5;
cout << "Please type some characters: " << endl;
cin.get(word, n);
cout << "the 1st " << n - 1
<< " characters typed: "
<< word << endl << endl;
}
int main()
{
get0();
get1();
}
#include <iostream> /// cin, cout
#include <climits> /// INT_MAX
usingnamespace std;
/// test the get(c) stream member function
void get0()
{
char c;
cout << "Please type some characters: ";
cin.get(c);
cout << "the 1st "
<< " character typed: "
<< c << endl << endl;
}
/// test the get(p, n) stream member function
void get1()
{
char word[10];
int n = 5;
/// Skip the rest of the buffer including the newline.
cin.ignore(INT_MAX, '\n');
cout << "Please type some characters: ";
cin.get(word, n);
cout << "the 1st " << n - 1
<< " characters typed: "
<< word << endl << endl;
}
int main()
{
get0();
get1();
}