cin.ignore() question?

I have read many different programming books over the last couple of years, then stop programming. I just pick up a class in college on programming and I been reading through this book and cant seem to understand what cin.ignore(); does, Here is an example code from the book maybe someone can help me understand it better. Any help would be great thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main( ) 
{
    int a = 23;
    int b = 34;
    
    cout << "line 3: Enter a number followed by a character: ";
    cin >> a >> b;
    
    cout << endl << "Line 5: a = " << a << ", b = " << b << endl;
    
    cin.clear();
    
    cin.ignore( 200, '\n' );
    
    cout << "line 8: enter two numbers: ";
    cin >> a >> b;
    cout << endl << "Line 10: a = " << a << ", b = " << b << endl;
    
    system( "pause" );
    return( 0 );
}
Extracts characters from the input sequence and discards them.

see http://www.cplusplus.com/reference/iostream/istream/ignore/
Thanks I think i get it a little better then I did.
Argh! A college textbook shows how to use cin.ignore() and still uses system( "pause" ), and even without #including <cstdlib>! Nothing like ruining young minds right from the beginning!

:-@

How to "pause"
http://www.cplusplus.com/forum/articles/7312/

Why system() is evil
http://www.cplusplus.com/forum/articles/11153/
The only reason I'm using system( "pause" ); because of dev C++ The book has nothing on system("pause" ); but if you have a better compiler i can use for free i be more then happy to use that one.
Last edited on
Ah, see my link above.

I was just laughing because a lot of C and C++ textbooks really do use system("pause"), and a lot of college professors do to. It is a shame, really.

Your compiler is the GCC, MinGW version, albeit an older version. You are fine with that until you start getting into some more advanced/modern stuff. Dev-C++ is reportedly a nice beginner solution. (I've never used it personally.)


But if you want something different, you might check out Code::Blocks, which is a really nice cross-platform IDE for multiple languages. Looky looky what I just googled:
http://www.cprogramming.com/code_blocks/
Follow the instructions to it set up using a recent version of MinGW.


To answer your original question more fully, what istream::ignore() does is just skip input until either
  - a specific character is encountered
  - a specific number of characters have been examined
It could have been named something like "skip" or "get_until" or somesuch.

Hope this helps.
Thanks for the help..
Just to make sure I'm understand this don't use system( "pause" ); and cin.ignore(); ?
the funny thing with ignore() is: when u use it with a few, lets say 100, numbers of characters to skip and u want the user to press a button to exit he has to press the button multiple times, till it works... :P...
What?
1
2
3
4
5
6
7
8
#include <iostream>
#include <limits>

void PressEnterToContinue()
  {
  std::cout << "Press ENTER to continue... " << flush;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  }



if i use this example... i need to click the Enter-Button multiple times, till it works...
Using that method, you need to press ENTER at most one time.

If your code dinks with the standard input (cin), then you can booger it so that you don't have to press ENTER at all.

If your program is having to press ENTER more than once to escape, then it is your program that is doing something that requires the user to press ENTER more than once.

Post your code if you need more help.
Topic archived. No new replies allowed.