Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cin before fgets

Reply
Thread Tools

cin before fgets

 
 
Charles Wilkins
Guest
Posts: n/a
 
      08-07-2003
given the following code snippet:

int num;

for (; {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin

how can i set the state of stdin so that after cin is called, fgets
blocks as expected?

Charles
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-07-2003

"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
news:(E-Mail Removed)...
> given the following code snippet:
>
> int num;
>
> for (; {
> cout << "enter number: ";
> cin >> num;
> //cin.clear();
>
> while (fgets(line, MAXLINE, stdin) != NULL) {
>
> after input is performed by cin, fgets doesnt block whereas if cin was
> not called, then fgets would block until it receives input from stdin
>
> how can i set the state of stdin so that after cin is called, fgets
> blocks as expected?
>
> Charles


What's expected? It doesn't block because there is still an unread newline
after the number. I/O in C and C++ is character based not line based. Use

cin.ignore(INT_MAX, '\n');

to ignore all pending input up to the next newline. #include <limits.h> for
the INT_MAX constant.

john




 
Reply With Quote
 
 
 
 
Charles Wilkins
Guest
Posts: n/a
 
      08-07-2003
On Thu, 7 Aug 2003 09:12:02 +0100, "John Harrison"
<(E-Mail Removed)> wrote:

>
>"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
>news:(E-Mail Removed).. .
>> given the following code snippet:
>>
>> int num;
>>
>> for (; {
>> cout << "enter number: ";
>> cin >> num;
>> //cin.clear();
>>
>> while (fgets(line, MAXLINE, stdin) != NULL) {
>>
>> after input is performed by cin, fgets doesnt block whereas if cin was
>> not called, then fgets would block until it receives input from stdin
>>
>> how can i set the state of stdin so that after cin is called, fgets
>> blocks as expected?
>>
>> Charles

>
>What's expected? It doesn't block because there is still an unread newline
>after the number. I/O in C and C++ is character based not line based. Use
>
>cin.ignore(INT_MAX, '\n');
>
>to ignore all pending input up to the next newline. #include <limits.h> for
>the INT_MAX constant.
>
>john
>
>
>

this did what i was looking for. thank you.
 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      08-07-2003
Charles Wilkins wrote:
>
> given the following code snippet:
>
> int num;
>
> for (; {
> cout << "enter number: ";
> cin >> num;
> //cin.clear();
>
> while (fgets(line, MAXLINE, stdin) != NULL) {
>
> after input is performed by cin, fgets doesnt block whereas if cin was
> not called, then fgets would block until it receives input from stdin


Aside from the comments made else-thread, a more fundumental question
is, why are you mixing stream and file I/O paradigms? Why not just use
one or the other?

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
 
Reply With Quote
 
Charles Wilkins
Guest
Posts: n/a
 
      08-07-2003
On Thu, 07 Aug 2003 08:55:24 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (tom_usenet)
wrote:

>On Thu, 07 Aug 2003 03:33:48 -0400, Charles Wilkins
><2boxers_at_comcast.net> wrote:
>
>>given the following code snippet:
>>
>>int num;
>>
>>for (; {
>> cout << "enter number: ";
>> cin >> num;
>> //cin.clear();
>>
>> while (fgets(line, MAXLINE, stdin) != NULL) {
>>
>>after input is performed by cin, fgets doesnt block whereas if cin was
>>not called, then fgets would block until it receives input from stdin
>>
>>how can i set the state of stdin so that after cin is called, fgets
>>blocks as expected?

>
>The problem is that, after your call to cin >> num, you've left a
>newline character in the stream, which fgets reads in immediately and
>returns. So all you need to do if empty that newline from the stream:
>
>cin >> num;
>cin.ignore(std::numeric_limits<int>::max(), '\n');
>
>Also, why are you mixing C stdio and iostreams? It will work, but
>what's wrong with getline? e.g.
>
>std::string line;
>while (std::getline(std::cin, line)) {
>
>Note you still need the ignore code, since whether you are using stdio
>or iostreams, the rest of the last line that you passed to the
>terminal will still be there.
>
>Tom

nothing is wrong with getline.
the fgets loop was part of an older model.
i simply was adding to this model as part of an exercise. for this
purpose I chose cin as a matter of preference.

beyond the scope of this experiment / exercise, i do not intentionally
seek to mix C stdio and iostreams

thanks,
Charles
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cin.get() and cin.eof() ends up in an endless loop Fernando C++ 4 11-16-2011 07:19 PM
cin and cin.getline() Aleander C++ 5 03-06-2005 03:57 PM
fgets and newline Mike Mimic C++ 4 05-15-2004 06:19 AM
std::cin.ignore() and std::cin.clear() Chris Mantoulidis C++ 5 01-06-2004 11:08 PM
cin and cin.clear() problem TaiwanNoWhere C++ 8 10-17-2003 05:53 PM



Advertisments