Reply
Thread Tools Display Modes
#1
Old 11-27-2009, 11:46 AM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
C++ Question: ignore(100,'/n');

Here is my question: The ignore function given in title should be to ignore the first 100 characters, which will in fact, be consumed, after they are entered. The /n will also be consumed, and a new line will commence. So, why on earth do I want to ignore 100 characters? If I want to ignore 100 characters, what happens if I get the right answer within those 100 characters? Do I have to type in 100 characters before I type in the correct answer? I'm mystified by this.
Can anyone explain this to me?
Thank you,
hh
#2
Old 11-27-2009, 11:52 AM
Guest
Join Date: Jun 2008
Posts: 311
Perhaps you could provide a little more context. Where are you finding this function? Are you supposed to be writing this for a homework assignment?

Also, that should be '\n', not '/n'. Backslash is the escape character in most modern languages.
#3
Old 11-27-2009, 11:55 AM
Charter Member
Moderator
Join Date: Jan 2000
Location: The Land of Cleves
Posts: 73,158
Without any further context, I can speculate that you might be reading in data from a data file, and that the first 100 characters are a header that's put in for human readability, which the computer program doesn't need. They might, for instance, be reminders to the human reader of what each column of data represents (of course, the program never forgot in the first place).
#4
Old 11-27-2009, 12:42 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by QuercusMax View Post
Perhaps you could provide a little more context. Where are you finding this function? Are you supposed to be writing this for a homework assignment?

Also, that should be '\n', not '/n'. Backslash is the escape character in most modern languages.
This was a textbook example of the ignore function, and their explanation of it, tied in with an explanation of the getting info from a keyboard, the keyboard buffer, and the getline function. I'm afraid that on a test, the instructor will ask me to do X, and it will be related to this function, but I won't know why/what to enter, because I am in the dark about this. The example is from the book, and the corresponding Powerpoint, which is by the same people, so the PP is worthless, in this context. I'm trying to understand what is going on... A few lines from the book:

SYNTAX: cin.ignore([numberOfCharacters] [,delimiterCharacter]);

"As the syntax indicates, the ignore() function has two optional arguments: numberOfCharacters and delimiterCharacter. The numberOfcharacters argument is an integer that represents the maximum number of characters the function should consume. The delimiterCharacter argument is a character that, when consumed, stops the ignore() function stops from reading and discarding any additional characters. the ignore() function stops reading and discarding characters either when it consumes the number of characters specified in the numberOfCharacter argument or when it consumes the delimiterCharacter, whichever occurs first. If you omit the numberOfCharacters argument, the default number of characters to consume is 1. "
So, I am reading that we are wanting to consume 100 characters. I have no idea why. My initial understanding was because we want to limit it to the reasonable number of characters in an input, e.g., if I want a zip code, I don't want the entry to be 16 or more characters. But, I got to thinking that I don't want 100, or however many, to be consumed...I want the first X number to be entered, and any after that to be discarded.
Am I even coherent? This has been a stumbling block for me, and I may not be as clear as I want. I want to know under what circumstances to enter the syntax above, and what will be happening in the operation of the program.

Thanks,
hh

Last edited by handsomeharry; 11-27-2009 at 12:47 PM.
#5
Old 11-27-2009, 01:06 PM
Guest
Join Date: Jun 2008
Posts: 311
I think you're *way* overthinking it.

It's really very simple. There's not much to it. All it does it read a certain number of characters, and throw them away. I think Chronos had it right - you might use this where you're reading from a file, want to just ignore the first line. Or maybe you're reading a number of fields, separated by some delimiter, and don't care about one of them. You could either read it into a temp variable and throw it away, or just call ignore().

That's all. Seriously.
#6
Old 11-27-2009, 01:17 PM
BANNED
Join Date: Jan 2003
Location: New York City
Posts: 2,859
Also, you'd probably use either the 'numberOfCharacters' argument, OR the 'delimiter' argument, but not both. You either want to throw out a set number of characters, or all characters up to a special one. If you're just interested in the delimiter, then you would make the 'numberOfCharacters' argument very large. 100 is longer than an 80 char line of text, for example. cin.ignore(100,'\n') would simply mean, "ignore the first line."
#7
Old 11-27-2009, 02:56 PM
Guest
Join Date: Nov 2004
Location: Seattle, WA
Posts: 2,009
I agree with Quercus: you're probably overthinking things. The ignore() function ignores the number of characters you specify, optionally stopping at a given delimiter. If there is any possibility that there will be something you might care about in those characters, you should not use the ignore function.
#8
Old 11-27-2009, 03:03 PM
Guest
Join Date: Oct 1999
Location: I'm right here!
Posts: 8,858
Maybe an example application will help: You're reading in a file with numerical data given in ASCII, and you want to allow users to add comment lines to the data. You require data lines to start with a space, and comments to start with a #. You read the first character, and if it's a #, you use ignore to read and discard the rest of the line. The 100 just means the comment lines can only be up to 100 characters long.
#9
Old 11-28-2009, 07:57 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
I'm still overthinking!!!! <sorry> Thanks everybody, for your great help, but it's my nature to overthink, and go down false paths! I can't help it::sheepish, dumb grin::

I'm still lost, but I may be in the correct forest.
I can show part of the program to show what i'm looking at, and what I was expecting, and why I am confused.

{yada, yada, yada....


cout<<"Enter sales amount: ";
cin>>sales amount;
cin.ignore(100, "\n");

OK. I'm expecting numbers to be entered. Why should I ignore the first numbers? I am expecting X numbers to be entered, per the cin>>sales amount line. The next thing I know, I'm throwing the first numbers out! EEK! Am I expecting the entry person to enter something totally weird for the first 100 times, and then straighten up?
If he gets the first 90 correct, his answer will be discarded?
I've read everything you all wrote, and am grateful, but I'm still missing it.
I thought that I would be able to follow you, ZenBeam, but you started going into ASCII, and I'm not really that well versed. I know the difference, but in regard to this program, you just lost me. This is the first chapter after I wrote/made a first, elementary program.
Can you all get me back online?
Thanks again, everybody.

hh

Last edited by handsomeharry; 11-28-2009 at 08:02 PM.
#10
Old 11-28-2009, 08:07 PM
Guest
Join Date: Jul 2000
Location: Philadelphia, PA
Posts: 1,183
What the code you posted does is unusual. One would not normally ignore input directly typed in by the user. The function is more useful in the context of processing file input. Is it from your textbook, and is it an example or a challenge problem?
#11
Old 11-28-2009, 08:16 PM
Guest
Join Date: Jul 2000
Location: Philadelphia, PA
Posts: 1,183
Also, what do you mean by "expecting X numbers to be entered"? An extraction from cin will only read one "number", either as a numeric value if the "sales amount" (I'm assuming the space in the variable name is a typo) variable is an int, double, or similar, or as a string up to the first whitespace in the input if the variable is a string.
#12
Old 11-28-2009, 08:18 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Cleophus View Post
What the code you posted does is unusual. One would not normally ignore input directly typed in by the user. The function is more useful in the context of processing file input. Is it from your textbook, and is it an example or a challenge problem?
(Oh, great, I go down enough false paths, and I now have to read some freak textbook authors strange code!)
Yes, it's from the Powerpoint by the author. Hold on....just checked, and it's in the textbook also. It's just an example of a program to display sales per state.
Thanks for your quick response.

hh
#13
Old 11-28-2009, 08:21 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Cleophus View Post
Also, what do you mean by "expecting X numbers to be entered"? An extraction from cin will only read one "number", either as a numeric value if the "sales amount" (I'm assuming the space in the variable name is a typo) variable is an int, double, or similar, or as a string up to the first whitespace in the input if the variable is a string.
Sorry, I meant 'expecting X characters (representing sales e.g. 259.00) to be entered." You are correct in the typo.
Thanks,
hh
#14
Old 11-28-2009, 08:32 PM
BANNED
Join Date: Jan 2003
Location: New York City
Posts: 2,859
Does this "textbook author" and "powerpoint presenter" also happen to be your "professor"? Those usually turn out to be the worst books.
#15
Old 11-28-2009, 08:37 PM
Guest
Join Date: Oct 1999
Location: I'm right here!
Posts: 8,858
ASCII just means text (as opposed to binary). So a data file you're trying to read data from might look like below. I'm assuming the first character of each line is read, and if it's '#', cin.ignore() is called. Only the three lines of three numbers will be read and used.
Code:
# This is just test data. cin.ignore() will ignore these lines,
# but will read the two lines following.
 0.0000 10.3000 99.300
 1.0000 14.2200 8.7100
# This is another comment. cin.ignore() will ignore this line also.
# Another comment, followed by more data.
 2.0000 34.2 111.00
# One last comment line.
#16
Old 11-28-2009, 08:38 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Alex_Dubinsky View Post
Does this "textbook author" and "powerpoint presenter" also happen to be your "professor"? Those usually turn out to be the worst books.
No, surprisingly. I've been there, and you are 100 percent correct!

hh
#17
Old 11-28-2009, 08:45 PM
Guest
Join Date: Jul 2000
Location: Philadelphia, PA
Posts: 1,183
I think I know what it's (attempting) to do. It eats up the newline at the end of the previous line of input, but the first parameter of 100 is unneeded and the second parameter is overly specific and arguably redundant. ignore() with no parameters would accomplish the same thing. Is getline() used after the fragment you showed us?

Last edited by Cleophus; 11-28-2009 at 08:46 PM.
#18
Old 11-28-2009, 08:47 PM
Member
Join Date: Mar 2002
Location: Southern Pennsylvania
Posts: 21,599
I never used cin when I worked in C++ but...

Is it possible to redirect the cout from one program to the cin of another? Back in my unix days programmers did this quite a bit in C and would chain programs together to parse out certain input. Perhaps cin.ignore is used when doing something similar.
#19
Old 11-28-2009, 08:52 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by ZenBeam View Post
ASCII just means text (as opposed to binary). So a data file you're trying to read data from might look like below. I'm assuming the first character of each line is read, and if it's '#', cin.ignore() is called. Only the three lines of three numbers will be read and used.
Code:
# This is just test data. cin.ignore() will ignore these lines,
# but will read the two lines following.
 0.0000 10.3000 99.300
 1.0000 14.2200 8.7100
# This is another comment. cin.ignore() will ignore this line also.
# Another comment, followed by more data.
 2.0000 34.2 111.00
# One last comment line.
OK, so in my example, the person entering the data in response to cin>>salesamt; will type all of your text, and my program will only read the numerical data?

Or, is this just another way to express // in code?

Or, will I enter everything that you just wrote?

Also, to clarify what you are saying, when you say 'a data file that you're trying to read from" do you mean "program that the computer is to perform?"
I know that my question is being a bit too, what, nitpicky, but if you see the way I spazzed out on the word 'algorithm' on another thread some time back, you'll understand that I am a bit at sea on the nomenclature of programming.
(Hard to imagine that I worked for a year as an HTML coder, isn't it??)

<Chris Farley voice::I'm an idiot!! Idiot!!::end CF voice>

Thanks,
hh
#20
Old 11-28-2009, 08:59 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Cleophus View Post
I think I know what it's (attempting) to do. It eats up the newline at the end of the previous line of input, but the first parameter of 100 is unneeded and the second parameter is overly specific and arguably redundant. ignore() with no parameters would accomplish the same thing. Is getline() used after the fragment you showed us?
Actually, yes! Hold on...
cout<<"Enter State name: ";
getline(cin.statename);

Can you rephrase what you said in the above? I'm almost there, maybe.

hh
#21
Old 11-28-2009, 09:18 PM
Guest
Join Date: Oct 1999
Location: I'm right here!
Posts: 8,858
Sorry, I was imagining reading from a file. Frankly, as Cleophus noted, that cin>>salesamt; example is odd. I think a more "real world" example would use reading from a file. Something like the following, where file.dat is a file containing the data and comments from my last post.

(I'm wondering if you've gotten to reading from files yet? fin below is just like cin, except it's reading from a file instead of the command line.)

Code:
 float a, b, c;
 char ch;
 fstream fin("file.dat",ios::in);
 while (!fin.eof()) {
 ch = fin.getc();
 if (ch == '#') {
 fin.ignore(100,'\n');
 } else {
 fin >> a >> b >> c;
//
// Additional processing...
//
...
 }
 }
...
* I'm not sure if getc() is the right function for reading a single character. I never do that.

In the code snip, each time through the loop handles one line of the data file. The while(!fin.eof()) is just checking whether we've read to the end of the file. Only the three data lines are read into a, b, and c and processed.
#22
Old 11-28-2009, 09:19 PM
Guest
Join Date: Jul 2000
Location: Philadelphia, PA
Posts: 1,183
Whenever a user types into standard input and presses the enter/return key, the special newline character is added to the end of the line, like so
User types -> hello
The line sent to your program in the standard input stream: "hello\n", where \n represents the newline character.

When cin is used, the newline remains in the input stream. If getline is used afterward, the function will read the newline and immediately terminate, placing only the newline in the variable given to getline. For getline to work as expected after using cin, the newline character must be ignored. cin.ignore() (note the empty parameter list) will do the job. It seems your textbook author wanted to explicitly specify a newline is to be ignored, and in order to do so both parameters must be specified, and 100 is probably a "magic"/BS value that was added to satisfy C++ syntax rules.
#23
Old 11-28-2009, 11:15 PM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Cleophus View Post
Whenever a user types into standard input and presses the enter/return key, the special newline character is added to the end of the line, like so
User types -> hello
The line sent to your program in the standard input stream: "hello\n", where \n represents the newline character.

When cin is used, the newline remains in the input stream. If getline is used afterward, the function will read the newline and immediately terminate, placing only the newline in the variable given to getline. For getline to work as expected after using cin, the newline character must be ignored. cin.ignore() (note the empty parameter list) will do the job. It seems your textbook author wanted to explicitly specify a newline is to be ignored, and in order to do so both parameters must be specified, and 100 is probably a "magic"/BS value that was added to satisfy C++ syntax rules.
So, I'm basically trying to 'ignore' information that is sent as part of some sort of normal operation, rather than trying to ignore the data entered from the user?
Also, the 100 is giving room for the rest of the info that getline will be receiving?
thanks,

hh
#24
Old 11-28-2009, 11:29 PM
BANNED
Join Date: Jan 2003
Location: New York City
Posts: 2,859
Quote:
Originally Posted by handsomeharry View Post
So, I'm basically trying to 'ignore' information that is sent as part of some sort of normal operation, rather than trying to ignore the data entered from the user?
Also, the 100 is giving room for the rest of the info that getline will be receiving?
thanks,

hh
cin left a newline on the stream. you should call ignore(1) to get rid of it. ignore(100,'\n') is a uselessly roundabout way to do the same. If you don't get rid of the newline, getline() will be tripped up.

If you're so confused about a piece of code, why don't you just type it up and see how it works? Comment out pieces of it, etc. This isn't 1973. You don't have to stand in line to be allocated 5 milliseconds on a mainframe. Figuring out how code works just through the power of thought can be a challenge even for a pro (in this case, very few pro programmers use cin and know such trivia about it), so props to Cleophus for figuring this out.

Last edited by Alex_Dubinsky; 11-28-2009 at 11:32 PM.
#25
Old 11-29-2009, 08:50 AM
BANNED
Join Date: Aug 2001
Location: oklahoma city
Posts: 7,886
Quote:
Originally Posted by Alex_Dubinsky View Post
cin left a newline on the stream. you should call ignore(1) to get rid of it. ignore(100,'\n') is a uselessly roundabout way to do the same. If you don't get rid of the newline, getline() will be tripped up.

If you're so confused about a piece of code, why don't you just type it up and see how it works? Comment out pieces of it, etc. This isn't 1973. You don't have to stand in line to be allocated 5 milliseconds on a mainframe. Figuring out how code works just through the power of thought can be a challenge even for a pro (in this case, very few pro programmers use cin and know such trivia about it), so props to Cleophus for figuring this out.
Sounds good! Thanks, everybody!!

And, definitely, Bravo, Cleophus!
#26
Old 11-30-2009, 07:29 AM
Member
Join Date: Jan 2003
Location: The Tropics, not in US
Posts: 3,615
One thing to remember that this function can be used with any iostream class; Instead of thinking it just for human input, consider it for a file. A file could have header which you want to ignore, or some other padding (if you reading a raw stream for TCP/IP for examle).
Reply

Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:39 AM.

Copyright © 2017
Best Topics: gun in mouth bi vs di man pees outside kosher vegetarian bavarian creme donut brass knuckles damage cut brakes is asphyxiation painful rule lawyering 3 point landing refrigerator vents temporal dimensions is cheesecake pie boxing tie halloween doctor names chile country pronunciation what is undercoating female vader archeology wod red up eats habanero pepper buying coal online make yourself passout dental implants forum bisexual message board keebler elves jewish coca vs cocoa is poetry fiction elvira boobs count money faster opposite of colorblind a330 cockpit pronounce taoiseach who was the leader of the beatles pirelli p zero tire pressure best place to shoot yourself in the head to die jos a bank tailoring best raspy voice male singers candle heaters do they work explain a time you received excellent customer service and what made that experience so special. why i eyes ya cat how to change battery in timex expedition indiglo curse of oak island message board destination sort facility fedex how to get into pipefitters union japanese late night tv a flock of wah wahs how to unscare yourself joan crawford secret storm correct way to rack pool balls tilex daily shower review what if lincoln had allowed the south to secede tim conway elephant youtube ivory liquid hand soap replacement how to get mail postmarked the same day gap between teeth getting bigger whats your porn name bees in siding of house how to open mccormick pepper grinder how to get personal property back from ex middle eastern guys attractive swiss army watch batteries replacement ball peen hammer uses cant fall asleep on back cold as a witch's teat drivers license in two states face wax for men