473,513 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cin is eating my output

I have been looking at exceptions as I need to get better at using
them.

I came across an interesting effect, demonstrated below. When I
ctrl-Z the input to throw an ios_base::failure, the first line of
output from cout is lost, even if I clear() and flush(). The only way
I have found to get the first line to appear is to put a newline at
the start. It seems to be ignoring everything up to and including the
first newline.

Investigating further I noticed that a triggering an ios_base::failure
by entering a letter did not have the same effect, nor did a spurious
exception. This led me to a possible diagnosis:

1 cin is waiting for input terminated by a newline.
2 I press ctrl-Z triggering an ios_base::failure.
3 Despite the exception cin is still waiting for its input.
4 I pass some text to cout for output.
5 cout puts the text into the console I/O buffer.
6 cin thinks that the text is input.
7 cin takes text from the buffer until the first newline.
8 Having found a newline cin is satisfied and goes away.
9 Screen output can now operate normally.

I assume that the ability of cin to grab text from cout is because
they share the console I/O buffer. The other triggers did not have a
problem because cin got some input terminated by a newline before the
error was thrown and so was no longer waiting for input.

One thing I have not been able to explain is when I use cerr for
output the same effect happens despite cerr being unbuffered. Maybe
the shared buffer is at a lower level in the system.

I am using Win ME and Dev-C++ 4.9.8.0 with the compiler that comes
with it by default. I do not know if this effect is reproducible on
other systems.

Questions:

1 Is my diagnosis roughly correct?
2 Am I doing something stupid?
3 Is there a standard way to tell cin not to eat the output?

Currently I just add an extra '\n' to the front of the first cout, but
this does mean that the output is different depending on how the file
error was triggered and I would rather avoid that if possible.
rossum

#include <iostream>
#include <stdexcept>
#include <cstdlib>
int main() {
using std::cout;
using std::cin;

const int upper_bound = 10;
const int lower_bound = 2;

cout << "Enter an odd number between " << lower_bound << " and "
<< upper_bound << ": ";
int number = 0;

try {
// Use ctrl-Z to trigger the effect.
// Enter a letter to avoid the effect
if (!(cin >> number))
{ throw std::ios_base::failure("Error reading input stream.");
}
// Use 99 to throw a spurious file error - avoids the effect.
if (number == 99)
{ throw std::ios_base::failure("99 entered."); }
if (number % 2 == 0)
{ throw std::invalid_argument("Number is not odd."); }
if (number > upper_bound)
{ throw std::out_of_range("Number is too large."); }
if (number < lower_bound)
{ throw std::out_of_range("Number is too small."); }
} // end try

// File error needs special handling
catch (std::ios_base::failure& ferr) {
// cout.clear(); // No effect
// cout.flush(); // No effect
// cin.clear(); // No effect
cout << "This line does not appear on screen.\n" // \n at end
// cout << "\nThis line does appear on screen." // \n at start
// cout << "This doesn't appear.\nThis does." // \n in middle
// std::cerr << "Output through cerr\n" // Using cerr
<< '\n' << number << ": " << ferr.what() << '\n';
} // end catch

// Other errors do not need special handling
catch (std::exception& err) {
cout << number << ": " << err.what() << '\n';
} // end catch

return EXIT_SUCCESS;
} // end main()
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #1
4 1965
"rossum" <ro******@coldmail.com> wrote in message
news:q7********************************@4ax.com...
I have been looking at exceptions as I need to get better at using
them.

I came across an interesting effect, demonstrated below. When I
ctrl-Z the input to throw an ios_base::failure, the first line of
output from cout is lost, even if I clear() and flush(). The only way
I have found to get the first line to appear is to put a newline at
the start. It seems to be ignoring everything up to and including the
first newline.


I just tried reproducing your problem using Visual Studio 2005 beta 1 under
Windows XP SP2 and it does not. The line appears on screen as expected.
Because the MS-DOS prompt from Windows Me is hardly comparable to cmd.exe in
Windows NT derivatives, I also tried running your code under Windows 98,
where the problem did reproduce. One striking difference is that under
Win98, pressing CTRL-Z immediately stops the input, while under Windows XP,
pressing CTRL-Z just prints "^Z" on screen, and I still have to press enter
for the application to continue. It seems then to be something fundamentally
strange about the way Win9x/Me handle CTRL-Z.

Since that makes this a platform issue, and not a C++ issue, your probably
better off asking in a newsgroup where Windows is on-topic.

--
Unforgiven

Jul 22 '05 #2
On Wed, 6 Oct 2004 01:08:02 +0200, "Unforgiven"
<ja*******@hotmail.com> wrote:
"rossum" <ro******@coldmail.com> wrote in message
news:q7********************************@4ax.com.. .
I have been looking at exceptions as I need to get better at using
them.

I came across an interesting effect, demonstrated below. When I
ctrl-Z the input to throw an ios_base::failure, the first line of
output from cout is lost, even if I clear() and flush(). The only way
I have found to get the first line to appear is to put a newline at
the start. It seems to be ignoring everything up to and including the
first newline.


I just tried reproducing your problem using Visual Studio 2005 beta 1 under
Windows XP SP2 and it does not. The line appears on screen as expected.
Because the MS-DOS prompt from Windows Me is hardly comparable to cmd.exe in
Windows NT derivatives, I also tried running your code under Windows 98,
where the problem did reproduce. One striking difference is that under
Win98, pressing CTRL-Z immediately stops the input, while under Windows XP,
pressing CTRL-Z just prints "^Z" on screen, and I still have to press enter
for the application to continue. It seems then to be something fundamentally
strange about the way Win9x/Me handle CTRL-Z.

Since that makes this a platform issue, and not a C++ issue, your probably
better off asking in a newsgroup where Windows is on-topic.


Thanks for the info.

rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #3

"rossum" <ro******@coldmail.com> wrote in message
news:q7********************************@4ax.com...
I have been looking at exceptions as I need to get better at using
them.

I came across an interesting effect, demonstrated below. When I
ctrl-Z the input to throw an ios_base::failure, the first line of
output from cout is lost, even if I clear() and flush().


Is this a problem that can be fixed by calling cin.sync(); before using cin
to get input?
Jul 22 '05 #4
On Wed, 6 Oct 2004 16:31:11 -0400, "Joe C" <jk*****@bellsouth.net>
wrote:

"rossum" <ro******@coldmail.com> wrote in message
news:q7********************************@4ax.com.. .
I have been looking at exceptions as I need to get better at using
them.

I came across an interesting effect, demonstrated below. When I
ctrl-Z the input to throw an ios_base::failure, the first line of
output from cout is lost, even if I clear() and flush().


Is this a problem that can be fixed by calling cin.sync(); before using cin
to get input?

No joy I'm afraid, but thanks for the suggestion anyway. I suspect
that the solution will be an upgrade. (Looks at piggy bank and
wonders how much is in it.)

rossum
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3399
by: Bryan | last post by:
i have a batch file that contains these two lines: -- args.bat echo %1 echo %2 when i run args.bat from the command line it works correctly... notice that abc has quotes in the first...
3
1993
by: Bobby Torkington | last post by:
Hi all I'm running version 4.0.16 on solaris 8 as the backend for a web site serving around 12 million pages impressions a month. The performance of the server is monitored using orcollator. ...
2
1060
by: Bob | last post by:
I'm sure some of you are familliar with PTFB... http://www.bobos.demon.co.uk/par/PTFB.htm it's a wonderful little app that answers dialogue box questions for you when they pop up. I have...
2
1786
by: Just Me | last post by:
I do not get WM_KEYDOWN messages for arrow keys presses in my overridden WndProc for a picturebox control. Something must be eating them - anyone familiar with what's going on? Thanks
0
2051
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
1
2006
by: google | last post by:
Hello all- Apologies to everyone for what's probably a very inchoate and uninformed question, but I've been thrust into the position about having to learn more about PHP very quickly. (I'm also...
9
7830
by: Tomi Lindberg | last post by:
Hi, With the following function definition, is it possible to create an instance of class C outside the function f (and if it is, how)? And yes, I think this is one of those times when the real...
5
1770
by: Per B. Sederberg | last post by:
Hi Everybody: I'm having a difficult time figuring out a a memory use problem. I have a python program that makes use of numpy and also calls a small C module I wrote because part of the...
2
3358
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.