954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cin.get() & cin.ignore()?!

Could someone explain the use for both?

Ive got a rough idea that cin.get() pauses the program and cin.ignore() is to clear the input buffer. But could you give me examples when you'd use them and why?

Cheers.

Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

cin.ignore() I always learned is used to pause the program until the user strikes the enter key. (This makes it useful for getting data displayed with cout because without such a pause statement, the program would quit when done.)
cin.get() I am not familiar with, but I suspect that it may be getLine(cin, outputString) 's little brother. Don't take my word on that though; ask someone more experienced or consult a book.
Party on, Garth!

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

Thanks Garth, I reckon from previous programs that they both pause the program in someway. Someone shoot me down if Im wrong though?!

Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

Heres a program to add weight to my query....

#include <iostream>;

using namespace std;

int main()
{
    int width, length, height;
    int volume;

    cout << "Please enter Width"<< endl;
    cin >>width;
    cout << "Please enter Length"<< endl;
    cin >>length;
    cout << "Please enter Height"<< endl;
    cin >>height;

    volume = width*length*height;

    cout << "A floor "<<width<<" x "<<length<<" x "<<height<<" metres equals "<<volume<<"m3"<< endl;
    cin.ignore();
    cin.get();
    
}


Without either cin.ignore() or cin.get() the program wont pauses, it just races through.

Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 
Thanks Garth, I reckon from previous programs that they both pause the program in someway. Someone shoot me down if Im wrong though?!



Glad to have been of service. Um...My name is Gabriel, not Garth; I was just quotingWayne's World. Just putting that out there.:D

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

Oops, let's clear this up.

cin.get( ) does just what it says - it gets a character from the input stream. Any character, including whitespace (blank, tab, newline). You can use it two ways, for this simple purpose:

char ch;
cin.get( ch );
//or
ch = cin.get( );


There are other ways to use the get( ) function (in VC++ 2003, 6 templates are listed)

This differs from cin >> ch in that this method skips over whitespace, get( ) does not. Leaving whitespace characters in the input stream can trip you up if you don't pay attention.

Run this little sample. At each prompt, press a single character, then the enter key. You don't get to enter a second character, because the newline after your first input is still waiting to be processed. The second get( ) grabs that. Now uncomment the ignore( ) line, this will eat up the newline so that your second get( ) can do something useful.

#include <iostream>
using namespace std;

int main( )
{
	char ch;

	cout << "Enter a character (just one): ";
	cin.get( ch );
	cout << ch << endl;

	cout << "Enter another single character: ";
	//cin.ignore(1);
	ch = cin.get( );
	cout << ch << endl;

	return 0;
}


ignore( ) takes zero, one or two arguments. First is a maximum number of characters (to include whitespace) that it will ignore (throw away). Second argument is a delimiter - it will stop at that character if it encounters it before the maximum number of characters is reached. If you give no arguments, the default is 1 char or newline, whichever is first. You may give only a number argument, in which case the default delimiter is eof( ).

Hope this clears some confusion.
Val

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
char ch;
cin.get( ch );
//or
ch = cin.get( );

Close. The zero argument version of get returns int_type so that it can signal end-of-file with traits_type::eof(). It's directly equivalent to getchar from , which returns int rather than char to account for the potential unsignedness of the char type and the guaranteed negative value of EOF. This would be a more correct example:

char ch;
cin.get ( ch );

// or

int ch;
ch = cin.get();

>in VC++ 2003, 6 templates are listed
It's best to use the standard instead of any one compiler for information like this. In this case, VC++ 2003 is correct. get has six overloads.

>If you give no arguments, the default is 1 char or newline, whichever is first.
>You may give only a number argument, in which case the default delimiter is eof( ).
I'm curious. How can the second argument default to a newline if you use the default count, but traits_type::eof() if you don't? The default is set to traits_type::eof(), and remains that unless you specify something else. So if you want to stop on a newline, you must provide the two argument form. This is the correct way to ignore any number of characters up to a newline:

std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

This requires you to include , (for streamsize), and (for numeric_limits).

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>If you give no arguments, the default is 1 char or newline, whichever is first.
>You may give only a number argument, in which case the default delimiter is eof( ).
I'm curious. How can the second argument default to a newline if you use the default count, but traits_type::eof() if you don't? The default is set to traits_type::eof(), and remains that unless you specify something else. So if you want to stop on a newline, you must provide the two argument form.


Oops, fingers got ahead of brain.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

That makes a lot more sense Vmanes as to the specifics of both.

Is it normal practise to use them to pause programs for inputs and to display results also?

Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

LET US TAKE A BETTER EXAMPLE
IF YOU HAVE TO ENTER 2 DIFFERENT STRINGS
IF WE ENTER 1ST ONE AND PRES ENTER
IT INPUTS THE FIRST

AND ALSO LETS THAT THE SECOND STRING IS ALREADY ENTERED i.e THE FIRST STRING.......

SO BOTH STRING BECOMES SAME AND NEGLECTS THE SECOND INPUT STAGE.....

AND GIVE RESULT

>>>>TO PREVENT THIS<<<<

WE USE cin.ignored();
or cin.ignored(100,"\n"); // example

pankajkumar911
Newbie Poster
1 post since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

LET US TAKE A BETTER EXAMPLE IF YOU HAVE TO ENTER 2 DIFFERENT STRINGS IF WE ENTER 1ST ONE AND PRES ENTER IT INPUTS THE FIRST

AND ALSO LETS THAT THE SECOND STRING IS ALREADY ENTERED i.e THE FIRST STRING.......

SO BOTH STRING BECOMES SAME AND NEGLECTS THE SECOND INPUT STAGE.....

AND GIVE RESULT

>>>>TO PREVENT THIS<<<< WE USE cin.ignored(); or cin.ignored(100,"\n"); // example


That's a better example? :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You