cin.ignore statement
Whenever I would go to execute my program it would immedietly exit after the last cout statement was reached. I searched around a bit and found a site that said to use cin.ignore to remedy this. I have this code:
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
We just started learning C++ from Java and our instructor basically expects us to be able to program in the language with no book, just notes that she gives. Her notes mention nothing about the window closing problem, and she had no clue when someone asked about it.
So my question is this, what exactly does cin.ignore do? I've read that it will ignore up to n characters (256 in this case I believe), or up to a specified character c (the newline here I think). It seems that the program actually halts what it is doing and waits for an enter, although I know this can't be the case..since no key check is performed. How does the program know not to exit unless the enter key is pressed?
[1056 byte] By [
Dark Rain] at [2007-11-11 10:13:00]

# 1 Re: cin.ignore statement
the program halts on *any* sort of cin statement.
Ignore ignored up to N chars or until a token is found. So your ignoring up to 256 until a newline is found.
Enter is special and "submits" a typed console string to the program. Thats why it works. If you did cin.ignore and wanted an 'a' to be typed, you would need the enter key to submit the typed mess -- so you could type aaabbbccc and it would let you do so. To wait for an 'a' and discard the rest, you might loop doing a getch() until it is the 'a' for example.
jonnin at 2007-11-11 20:59:14 >

# 2 Re: cin.ignore statement
I think I'm starting to get it, but not fully yet. This is the last line of my program before that block of code posted above:
cout << feet << " feet equals " << inches << " inches. \n";
So your saying after this cout statement the cin.ignore statement is encountered and the program begins ingnoring typed characters, then proceeds to the cout message prints the "press enter.." string, and then encounters the cin.get() statment and halts. If I put cin.ignore(1, '/n') for instance, and type 10 a's, the program will accept it.
So that number doesn't really matter for my program does it? Because, when I hit enter the 10 a's are accepted and the program continues to the return 0 statement in which it exits..those ignored characters are never used. If I printed out the string typed later it would include 9 a's then, right?
Thanks for the help.
# 3 Re: cin.ignore statement
yes, the number of things ignored really doesnt matter to you, and if you printed it there would be the same string they typed less the first char. A good old fashioned cin to a string that you don't use would work just as well. I don't think I have ever used ignore, if that tells you anything.
jonnin at 2007-11-11 21:01:19 >

# 4 Re: cin.ignore statement
To delay the output onscreen, simply read a dummy character by cin at the end of the program:
char dummy;
cin>>dummy; //delay the output until a key is pressed
The whole cin.ignore() technique is overkill and I'm also surprised that your instructor doesn't refer you to a decent C++ textbook. Java and C++ are very different in spite of some superficial syntactic similarities. Either take the initiative and get a book or simple speak with the intructor about this.
Danny at 2007-11-11 21:02:19 >

# 5 Re: cin.ignore statement
Our instructors major before transferring to a computer science one was liberal arts I think if that tells you anything =p. Yeah I may have to get a book, thanks again for the help with this though.
# 6 Re: cin.ignore statement
In the first post the cin.get(); will delay If I'm not wrong .. I just think he just need to know what ignore() do not how to delay but after reading the post ..
Amahdy at 2007-11-11 21:04:23 >

# 7 Re: cin.ignore statement
Yeah I was just wondering what the purpose of the ignore() statement was in my original post, but I see it probably isn't needed.
# 8 Re: cin.ignore statement
Okey, btw have u found a good cpp book ? I have a huge library of softcopy of eBooks if u want, -I have not upload them in my website yet because of a trafiic problem- but if u want I recommend one for u and u can download it from here (http://w10.easy-share.com/805814.html) ..
and soon isa u can have the rest of those ebooks at my free ebook section .
U can also see my solutions for this book here at this forum, I haven't continued them yet -up to ch7 (arrays and strings)- but soon isa I'll post the rest .
Amahdy at 2007-11-11 21:06:25 >

# 9 Re: cin.ignore statement
Wow, thanks. I'll be sure to look for your library when you have it uploaded.