473,786 Members | 2,428 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::failu re, 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::failu re
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::failu re.
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_ar gument("Number is not odd."); }
if (number > upper_bound)
{ throw std::out_of_ran ge("Number is too large."); }
if (number < lower_bound)
{ throw std::out_of_ran ge("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 1984
"rossum" <ro******@coldm ail.com> wrote in message
news:q7******** *************** *********@4ax.c om...
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::failu re, 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*******@hotm ail.com> wrote:
"rossum" <ro******@coldm ail.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::failu re, 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******@coldm ail.com> wrote in message
news:q7******** *************** *********@4ax.c om...
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::failu re, 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*****@bellso uth.net>
wrote:

"rossum" <ro******@coldm ail.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::failu re, 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
3414
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 example and not in the second.
3
2006
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. I'm finding that the memory usage on the server remains pretty constant for weeks at a time at around 300 Meg but then every once in a while the size of the mysqld process jumps anywhere between another 50 and 250 Meg. It then stays at this new...
2
1070
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 an unavoiable need to use this in an application now, and I would prefer to code up something myself rather than tell a client that he needs to install PTFB. Does anyone have a pointer to dialogue-eating source code?
2
1801
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
2066
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 unsigned char #endif
1
2016
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 posting to alt.comp.lang.php, since I'm not sure what the difference is between the two groups.) Basically, I run a Web site that includes two PHP-based subsites -- one WordPress site and one PHPBB site. I get traffic that strikes me as
9
7845
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 question is why :) class C(object): def __init__(self): self.a = 'a'
5
1777
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 simulation needed to loop and I got a massive speedup by putting that loop in C. I'm basically manipulating a bunch of matrices, so nothing too fancy. That aside, when the simulation runs, it typically uses a relatively small amount of memory (about...
2
3380
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 @idUser int OUTPUT,
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6744
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.