•
•
•
•
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
![]() |
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.
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.
Heres a program to add weight to my query....
Without either cin.ignore() or cin.get() the program wont pauses, it just races through.
#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.........
•
•
•
•
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.
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.
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
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:
There are other ways to use the get( ) function (in VC++ 2003, 6 templates are listed)
This differs from
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.
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
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.
char ch; cin.get( ch ); //or ch = cin.get( );
char ch; cin.get ( ch ); // or int ch; ch = cin.get();
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' );
I'm here to prove you wrong.
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
>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.
>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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- cin.getline not working in for loop or...? (C++)
- cin.get problems in use (C)
- need help problem with cin.getline method (C++)
- cin.get issue (C++)
- blood sheds cin.get (confusing) (C++)
Other Threads in the C++ Forum
- Previous Thread: User defined functions
- Next Thread: Program runs but not visible



Linear Mode