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
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 ’
', not ‘/n’. Backslash is the escape character in most modern languages.
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).
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
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.
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,’
') would simply mean, “ignore the first line.”
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.
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.
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, "
");
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
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?
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.
(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
Sorry, I meant 'expecting X characters (representing sales e.g. 259.00) to be entered." You are correct in the typo.
Thanks,
hh
Does this “textbook author” and “powerpoint presenter” also happen to be your “professor”? Those usually turn out to be the worst books.
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.
# 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.
No, surprisingly. I’ve been there, and you are 100 percent correct!
hh
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?
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.
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
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