•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,334 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,161 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: 1720 | Replies: 17
![]() |
I am sure you meant
I wonder what you had just before logging in....
But I don't change my stmt:
Yes true, but if already used, why not make it a bit more er... robust...
[edit]Cripes, this Ravalon guy is everywhere...
[/edit]
cin >> I wonder what you had just before logging in....

•
•
•
•
Keep in mind, cin >> is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't!
Yes true, but if already used, why not make it a bit more er... robust...

[edit]Cripes, this Ravalon guy is everywhere...
[/edit] Last edited by ~s.o.s~ : Jan 2nd, 2007 at 2:24 pm.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
•
•
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 1
•
•
•
•
same problem as in old days. when you enter a number and press <Enter> cin does not remove the "\n" from the keyboard -- you have to do that
int x; cin >> x; cin.ignore(); // remove '\n' key
Thanks for the explanation. I still have a question. Now I can't see why the
little program below works.
#include <iostream>
using namespace std;
main()
{ int x,y;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "I read two values, " << x << " and "<< y << '\n';
}The first use of "cin" reads x but, according to your note, leaves a newline
character in the input buffer. The second call to "cin" should then see this
newline characteras the first character in the input buffer and therefore
stop reading. But it doesn't. What is the error in my reasoning?
Last edited by Ancient Dragon : Jan 6th, 2007 at 8:23 pm. Reason: corrected code tags
•
•
•
•
The first use of "cin" reads x but, according to your note, leaves a newline character in the input buffer. The second call to "cin" should then see this newline characteras the first character in the input buffer and therefore stop reading. But it doesn't. What is the error in my reasoning?
cin>> as that bloke who works great by himself but messes things up in a team. cin >> x does leave a newline character in the stream, but the first thing cin >> y does is remove any leading whitespace. The newline is thusly ignored without your intervention. Other input methods don't remove leading whitespace, and that's where the problem lies. It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 1
•
•
•
•
There's no error in your reasoning, and that's why this is probably the most irritating problem for people learning C/C++. Think ofcin>>as that bloke who works great by himself but messes things up in a team.cin >> xdoes leave a newline character in the stream, but the first thingcin >> ydoes is remove any leading whitespace. The newline is thusly ignored without your intervention. Other input methods don't remove leading whitespace, and that's where the problem lies.
So what's a poor programmer to do? Since it's apparently not possinble to know when this troubelsome newline char will appear would you recommend that cin.ignore() be used before any call to cin >>? If cin worked in a particular program with a particular set of input data without the ignore statement would it be safe to assume that it will work in the same program when it's run with different data?
Maybe resort to more robust methods of accepting input.
Just to rehash Mr. Ravalon's robust attempt at trying to solve the problem:
The combination of cin.clear( ) along with cin.ignore( ) seems to work in most cases but the above given solution appears to be more robust.
No, its never safe to assume anything as far as C++ is concerned since it hates people who take it for granted..
BTW the current software development senario is such that the process of getting input from console has been limited to programs which accept command line parameters. Nowadays its more like GUI everywhere, but still getting your concepts cleared on how actually the standard input stream of C++ works is a good thing.
Just to rehash Mr. Ravalon's robust attempt at trying to solve the problem:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <limits> #include <sstream> #include <stdexcept> #include <string> namespace Raye { using namespace std; template <class T, class U> T CType( U src ) { stringstream ss; T dst; if ( !( ss << src ) ) throw runtime_error( "CType: stringize error" ); if ( !( ss >> dst ) ) throw runtime_error( "CType: invalid conversion" ); return dst; } template <class T> T GetType( istream& is ) { string s; if ( !getline( is, s ) ) throw runtime_error( "GetInt: input failure" ); return CType<T>( s ); } } int main() { using namespace std; int age; string name; // Create the problem as an example cout << "Please type your age: "; cin >> age; cout << "Please type your name: "; getline( cin, name ); cout << name << " is " << age << " years old\n"; // Clean up the stream for an accurate test cin.ignore( numeric_limits<streamsize>::max(), '\n' ); // Eliminate the problem with GetType cout << "Please type your age: "; age = Raye::GetType<int>( cin ); cout << "Please type your name: "; getline( cin, name ); cout << name << " is " << age << " years old\n"; return 0; }
•
•
•
•
Since it's apparently not possinble to know when this troubelsome newline char will appear would you recommend that cin.ignore() be used before any call to cin >>?
•
•
•
•
If cin worked in a particular program with a particular set of input data without the ignore statement would it be safe to assume that it will work in the same program when it's run with different data?
No, its never safe to assume anything as far as C++ is concerned since it hates people who take it for granted..

BTW the current software development senario is such that the process of getting input from console has been limited to programs which accept command line parameters. Nowadays its more like GUI everywhere, but still getting your concepts cleared on how actually the standard input stream of C++ works is a good thing.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
'Don't use
Separate parsing from I/O so that you only work with strings when reading or writing. The code ends up being longer and more complicated, but it's easier to predict stream behaviour when it's only doing one thing at a time.
cin.ignore() for that purpose is a workaround, and if you're doing the right thing to begin with you shouldn't need it to clean up your mess. 
Assume nothing and you'll never be disappointed.
cin>>' seems to be the common consensus.
Separate parsing from I/O so that you only work with strings when reading or writing. The code ends up being longer and more complicated, but it's easier to predict stream behaviour when it's only doing one thing at a time.•
•
•
•
Since it's apparently not possinble to know when this troubelsome newline char will appear would you recommend that cin.ignore() be used before any call to cin >>?

•
•
•
•
If cin worked in a particular program with a particular set of input data without the ignore statement would it be safe to assume that it will work in the same program when it's run with different data?
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: How to make a C++ game??
- Next Thread: Carets in turbo c++ 3.0 graphics?



Linear Mode