473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

cin.ignore... better way wanted...

I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)

Thanks.

Oct 29 '06 #1
6 2711
sa***@murdocks.on.ca wrote:
I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)

Thanks.
How about replacing
"if there is anything in the input stream or buffer get rid of it"
with
"ignore the standard input stream, i.e. don't read from it at all"
?

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Oct 29 '06 #2

<sa***@murdocks.on.cawrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max);

extracts and discards characters from stream until eof or error.

-Mike
Oct 29 '06 #3
sa***@murdocks.on.ca wrote:
There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.
Maybe what you really want is "ignore until end of line"? You can use for
example:

while (cin && cin.get () != '\n')
continue;

The continue is just for clarity, an empty loop body leads to confusion.

But IMO the better way when you want input delimited by lines is to read
always with getline, and then parse the lines individually. Mixing << and
getline or ignore is confusing and error prone.

--
Salu2
Oct 29 '06 #4
On 29 Oct 2006 10:21:52 -0800 in comp.lang.c++, "sa***@murdocks.on.ca"
<sa***@murdocks.on.cawrote,
>There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.
I don't know how you got stuck in that pattern... I venture to guess
that most programs never use .ignore().

Many of my console programs go like:
Get a line of input with std:getline(cin, somestring);
Parse somestring, decide what to do with it (error msg if nonsense.)
Repeat until getline fails.

Often I use boost::regex to do the parsing - http://boost.org

Oct 29 '06 #5
=?ISO-8859-15?Q?Juli=E1n?= Albo:
while (cin && cin.get () != '\n')
continue;

The continue is just for clarity, an empty loop body leads to confusion.

If you think there's confusion, you have the choice of using comments.

while (cin && cin.get() != '\n') /* Empty Body */;
On the other hand, you could just trust that the reader knows how a loop
works.

--

Frederick Gotham
Oct 29 '06 #6
Frederick Gotham wrote:
>The continue is just for clarity, an empty loop body leads to confusion.
If you think there's confusion, you have the choice of using comments.
And I also have the choice of do it like I do.

--
Salu2
Oct 29 '06 #7

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

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
10
by: Nicolas Fleury | last post by:
Hi all, The part of the Python community that doesn't like @decorators needs to unite behind an alternative syntax to propose to Guido. I suggest we use this thread to try to do it. If you...
1
by: windandwaves | last post by:
Are you allowed to use the following syntax in Mysql 4.0.17-nt : INSERT DELAYED IGNORE INTO `mytable` ( `ID` , `A` , `B` ) VALUES ("'.session_id().'", "2", ROUND(NOW()/10000) ); I am...
1
by: temp | last post by:
Hi all, I know this is easy for you guys but I am not a VB developer. My boss just wanted me to generate a mapping of all the queries in our Access DB. So I managed to create some subroutines to...
23
by: FrancisC | last post by:
#include <stdio.h> int file_copy( char *oldname, char *newname ); int main() { char source, destination; printf("\nEnter source file: ");
5
by: ma740988 | last post by:
Consider the source: # include <iostream> # include <string> # include <fstream> # include <vector> # include <sstream> using namespace std;
3
by: toton | last post by:
Hi, Can I safely ignore the inline keyword without thinking of performance ? I need some better organization of my files, and need to shift some of my inlined functions to cpp file. I have to...
2
by: gregmcmullinjr | last post by:
I am wondering if there is a way to use a DTD or Schema to instruct an XML parser to ignore tags that are not defined. That is, if my list of acceptable tags is <bodyand <content>, then in the...
8
by: shira | last post by:
I have done a fair bit of searching, but haven't yet been able to find an explanation as to why one would set "ignore nulls" to "yes" when creating an index. I understand what it does (I think),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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,...
0
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...
0
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...
0
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...

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.