Newbie: what is cin.get()

Z

Zalek Bloom

I try to understand the cin object. The sample from the book:

#include <iostream.h>
#include <string.h>

int main()
{
char ch;
while ( (ch = cin.get()) != EOF)
{
cout << "ch: " << ch << endl;
}
cout << "\nDone\n";
return 0; */
}

Here is the output:

main <=== this is the value I entered
ch: m
ch: a
ch: i
ch: n
ch:

My question:
When c++ is waiting for my input?
I understand (I think) cin object but in this example cin appears in a
loop.

Any ideas what happens there?

Thanks,

Zalek
 
Z

Zalek Bloom

I try to understand the cin object. The sample from the book:

#include <iostream.h>
#include <string.h>

int main()
{
char ch;
while ( (ch = cin.get()) != 'x')
{
cout << "ch: " << ch << endl;
}
cout << "\nDone\n";
return 0; */
}

Here is the output:

mainx <=== this is the value I typed and pressed enter
ch: m
ch: a
ch: i
ch: n
ch:

My question:
When c++ is waiting for my input? What was the command that c++ waited
for my input?

Thanks,

Zalek
 
A

Alf P. Steinbach

I try to understand the cin object. The sample from the book:


The code is horrendous, non-standard, and incorrect even for a
pre-standard compiler: which book is that?
 
Z

Zalek Bloom

The code is horrendous, non-standard, and incorrect even for a
pre-standard compiler: which book is that?

Sams teach yourself C++ in 21 days by Jesse Liberty

Zalek
 
J

John Harrison

Zalek Bloom said:
I try to understand the cin object. The sample from the book:

#include <iostream.h>
#include <string.h>

int main()
{
char ch;
while ( (ch = cin.get()) != EOF)

Bad, bad code.

int ch;
while ( (ch = cin.get()) != EOF)

cin.get() returns an *int*, which is either the EOF value (usually -1) or a
value castable to a char.
{
cout << "ch: " << ch << endl;

cout << "ch: " << (char)ch << endl;

Now that ch is an int, you need to cast here to output as a char. The cast
is safe since you've already tested for EOF.
}
cout << "\nDone\n";
return 0; */
}

Here is the output:

main <=== this is the value I entered
ch: m
ch: a
ch: i
ch: n
ch:

My question:
When c++ is waiting for my input?
I understand (I think) cin object but in this example cin appears in a
loop.

Any ideas what happens there?

I'm not sure I understand your question. Maybe what is confusing you is
buffering. cin waits for input, you type main\n, this input is buffered,
which means it is saved for later reading. Each call to cin.get() reads a
single character from the buffer, so cin.get() does not have to wait again
until it has emptied the buffer.
Thanks,

Zalek

john
 
P

Peter van Merkerk

I try to understand the cin object. The sample from the book:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

From now on, know that 'teach yourself X in Y days' is a big red
warning flag, meaning the book is probably packed full of
misleading mistakes.

If title is misleading (unless someone seriously believes one can learn
C++ in just 21 days) I guess you can expect the same from its contents.
Accelerated C++ (http://www.acceleratedcpp.com/) is usually recommended
as the THE beginners book to have. It is worrying that there are so
many poor quality C++ books and tutorials around. I think C++ got at
least some of its bad reputation directly or indirectly from those
sources.
 
Z

Zalek Bloom

Bad, bad code.

int ch;
while ( (ch = cin.get()) != EOF)

cin.get() returns an *int*, which is either the EOF value (usually -1) or a
value castable to a char.


cout << "ch: " << (char)ch << endl;

Now that ch is an int, you need to cast here to output as a char. The cast
is safe since you've already tested for EOF.


I'm not sure I understand your question. Maybe what is confusing you is
buffering. cin waits for input, you type main\n, this input is buffered,
which means it is saved for later reading. Each call to cin.get() reads a
single character from the buffer, so cin.get() does not have to wait again
until it has emptied the buffer.

John,

I don't understand when c++ is waiting for a keybord input. If the
line "while ( (ch = cin.get()) != EOF)" causes waiting for the input -
this line is executed many times.
Here is the program:

void main()
{
char ch;
while ( (ch = cin.get()) != EOF)
{
cout << "\nch: " << ch ;
}
cout << "\nDone\n";
}

When I look on the output, the program was executed this way:

1.ch = cin.get())
2. test for EOF
3. cout << "\nch: " << ch ;
and again:
ch = cin.get())
test for EOF
cout << "\nch: " << ch ;

So when happen the one time event when I entered the "main" string and
pressed the enter key?

Zalek
 
T

Thomas Matthews

Zalek said:
I try to understand the cin object. The sample from the book:

#include <iostream.h>
#include <string.h>

int main()
{
char ch;
while ( (ch = cin.get()) != EOF)

This is a technique inherited from C. In C++, use the following:
while (cin.get(ch))
This syntax returns the stream, which is then tested for good or
failure (by using a void function).
{
cout << "ch: " << ch << endl;
}
cout << "\nDone\n";
return 0; */
}

Here is the output:

main <=== this is the value I entered
ch: m
ch: a
ch: i
ch: n
ch:

My question:
When c++ is waiting for my input?
I understand (I think) cin object but in this example cin appears in a
loop.

Many implementations buffer the input from the keyboard / console.
The characters are placed into a holding location until the [ENTER]
or [RETURN] key is pressed. This could be an operating system issue
that allows the user to edit the characters before handing them over
to the program.

If you want to bypass this "feature", your operating system or compiler
may provide functions for getting an unbuffered char from the console
or keyboard (provided they exist).
Any ideas what happens there?

Thanks,

Zalek


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
L

llewelly

Thomas Matthews said:
This is a technique inherited from C. In C++, use the following:
while (cin.get(ch))
This syntax returns the stream, which is then tested for good or
failure (by using a void function).
[snip]

'by using a void function' ? What do you mean by that? cin.get(ch)
returns an istream&, which is implicitly convertible to void*,
which can be tested for null (false) or non-null (true). Maybe you
confused that with a void function ?
 
L

llewelly

Peter van Merkerk said:
If title is misleading (unless someone seriously believes one can learn
C++ in just 21 days) I guess you can expect the same from its contents.
Accelerated C++ (http://www.acceleratedcpp.com/) is usually recommended
as the THE beginners book to have. It is worrying that there are so
many poor quality C++ books and tutorials around. I think C++ got at
least some of its bad reputation directly or indirectly from those
sources.

I would say not 'some', but 'most' - the people who dislike C++
are in my experience beset by all sorts of confusions, most of
which occur in poor C++ books.
 
J

John Harrison

John,

I don't understand when c++ is waiting for a keybord input. If the
line "while ( (ch = cin.get()) != EOF)" causes waiting for the input -
this line is executed many times.
Here is the program:

void main()
{
char ch;
while ( (ch = cin.get()) != EOF)
{
cout << "\nch: " << ch ;
}
cout << "\nDone\n";
}

When I look on the output, the program was executed this way:

1.ch = cin.get())
2. test for EOF
3. cout << "\nch: " << ch ;
and again:
ch = cin.get())
test for EOF
cout << "\nch: " << ch ;

So when happen the one time event when I entered the "main" string and
pressed the enter key?

Zalek

Well its like I tried to explain, cin.get() only waits for input if there is
no input already outstanding. Here's what happens

1) cin.get() is called, no input outstanding so cin.get() waits for you to
type something
2) You type main followed by newline, that's five characters.
3) cin.get() reads a *single* character 'm'. Waiting or not cin.get() only
ever reads a single character. However you typed in five characters, so four
are still left unread, they are 'in the buffer'.
4) You go round the loop
5) cin.get() is called again, but this time there are four characters left
unread, so cin.get() does not wait, it simply reads the next character 'a'
and returns. Now there are three unread characters left.

etc. etc.

John
 
K

Kevin Goodsell

Zalek said:
John,

I don't understand when c++ is waiting for a keybord input.

Well, let's stop right there for a minute. C++ doesn't ever really "wait
for a keyboard input." C++ has no concept of keyboards. It deals with
input and output as "streams". A stream is just a sequence of bytes.

Now, what happens is that cin is usually "attached" to your keyboard.
When you do something to cause cin to read data, and there's no data
immediately available, it will wait for data to become available.

In your case, you supplied more data than cin needed for the ".get()"
function, so the extra data stayed in the buffer. The next time a
".get()" was executed, it used that data that was still in the buffer,
rather than waiting for new data.
If the
line "while ( (ch = cin.get()) != EOF)" causes waiting for the input -
this line is executed many times.

It doesn't cause waiting. It only causes reading a byte. If no byte is
available, then waiting will occur.
Here is the program:

You are missing several header files.
void main()

This is wrong. main returns int, not void. This is required by the C++
standard. void has never been an acceptable return type for main.
{
char ch;
while ( (ch = cin.get()) != EOF)

As others have pointed out, this is also wrong. ch needs to be an int
for this to work as expected.

Also, the full name of cin is std::cin. You need to supply the std::
somehow (possibly with "using namespace std;" or "using std::cin;", or
by using the full name when referring to cin).
{
cout << "\nch: " << ch ;
}
cout << "\nDone\n";
}

When I look on the output, the program was executed this way:

1.ch = cin.get())
2. test for EOF
3. cout << "\nch: " << ch ;
and again:
ch = cin.get())
test for EOF
cout << "\nch: " << ch ;

So when happen the one time event when I entered the "main" string and
pressed the enter key?

The first time cin.get was executed.

-Kevin
 
T

Thomas Matthews

llewelly said:
This is a technique inherited from C. In C++, use the following:
while (cin.get(ch))
This syntax returns the stream, which is then tested for good or
failure (by using a void function).

[snip]

'by using a void function' ? What do you mean by that? cin.get(ch)
returns an istream&, which is implicitly convertible to void*,
which can be tested for null (false) or non-null (true). Maybe you
confused that with a void function ?

Perhaps I should have said:
This syntax returns the stream which is tested for good or
failure (using a function that returns "void *").

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Christopher

2nd edition, page 474

Zalek

Yea, I own the third edition. I thought mine was pretty good at explaining
things even with all the "21 days" protest. I guess yours is quite a bit
older. I still gotta check out "accelerated c++" though, maybe trade in my
other 8 useless books.
,
Christopher
 
J

Jesse Liberty

I'm afraid you are using an edition that is hopelessly out of date. This
code is totally rewritten in the current (4th) edition.
 
J

Jesse Liberty

We are now in our 4th edition, and I've worked very hard not only to keep
the new editions up to date, but to eliminate all errors. In addition, I
provide source code, a FAQ and an errata sheet on my web site.

As for the name, that is just marketing. Would you be offended by the (less
marketable but exactly equal) name "Teach Yourself C++ In 21 Chapters?" It
is certainly possible to read each of these chapters in a day, though of
course if you work at it it is unlikely you'll learn what you need to know
in three weeks.

It is like the old joke by Steven Wright: I went to the 24 hour store. They
were closed. I said "Hey, you're supposed to be open 24 hours" the guy said
"not in a row."

-j

--
Jesse Liberty, President
Liberty Associates, Inc.
http://www.LibertyAssociates.com


"> > > Sams teach yourself C++ in 21 days by Jesse Liberty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top