User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,497 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4516 | Replies: 8
Reply
Join Date: Jun 2007
Posts: 55
Reputation: Run.[it] is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

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

  #1  
Oct 16th, 2007
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.
.........scaricamento.........
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: Evanston IL
Posts: 132
Reputation: venomlash is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

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

  #2  
Oct 16th, 2007
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!
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Reply With Quote  
Join Date: Jun 2007
Posts: 55
Reputation: Run.[it] is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

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

  #3  
Oct 16th, 2007
Thanks Garth, I reckon from previous programs that they both pause the program in someway. Someone shoot me down if Im wrong though?!
.........scaricamento.........
Reply With Quote  
Join Date: Jun 2007
Posts: 55
Reputation: Run.[it] is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

Solution Re: cin.get() & cin.ignore()?!

  #4  
Oct 16th, 2007
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.
.........scaricamento.........
Reply With Quote  
Join Date: Oct 2006
Location: Evanston IL
Posts: 132
Reputation: venomlash is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

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

  #5  
Oct 16th, 2007
Originally Posted by Run.[it] View Post
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.
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

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

  #6  
Oct 17th, 2007
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
I am in mourning for my country.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

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

  #7  
Oct 17th, 2007
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).
I'm here to prove you wrong.
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

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

  #8  
Oct 17th, 2007
>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.
I am in mourning for my country.
Reply With Quote  
Join Date: Jun 2007
Posts: 55
Reputation: Run.[it] is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Run.[it]'s Avatar
Run.[it] Run.[it] is offline Offline
Junior Poster in Training

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

  #9  
Oct 17th, 2007
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?
.........scaricamento.........
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC