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.

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!

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

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.

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 quoting Wayne's World. Just putting that out there.:D

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

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 <cstdio>, 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 <iostream>, <ios> (for streamsize), and <limits> (for numeric_limits).

>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.

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?

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

commented: Please read the date of the last post: 2007. -4

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.