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 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
Reply
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: It's cinfull!

  #11  
Jan 2nd, 2007
I am sure you meant 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!
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]
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.
Reply With Quote  
Join Date: May 2006
Posts: 2,724
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: It's cinfull!

  #12  
Jan 2nd, 2007
Whatever!
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: It's cinfull!

  #13  
Jan 6th, 2007
Originally Posted by Ancient Dragon View Post
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
Reply With Quote  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Rep Power: 2
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: It's cinfull!

  #14  
Jan 6th, 2007
Originally Posted by murschech View Post
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?
There's no error in your reasoning, and that's why this is probably the most irritating problem for people learning C/C++. Think of 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.
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 152
Lerner Lerner is offline Offline
Veteran Poster

Re: It's cinfull!

  #15  
Jan 6th, 2007
>>Think of cin>> as that bloke who works great by himself but messes things up in a team.

I like that analagy. Well done!
Reply With Quote  
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: It's cinfull!

  #16  
Jan 7th, 2007
Originally Posted by Ravalon View Post
There's no error in your reasoning, and that's why this is probably the most irritating problem for people learning C/C++. Think of 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.


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?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: It's cinfull!

  #17  
Jan 7th, 2007
Originally Posted by murschech View Post
So what's a poor programmer to do?
Maybe resort to more robust methods of accepting input.
Just to rehash Mr. Ravalon's robust attempt at trying to solve the problem:
  1. #include <iostream>
  2. #include <limits>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <string>
  6.  
  7. namespace Raye {
  8. using namespace std;
  9.  
  10. template <class T, class U>
  11. T CType( U src )
  12. {
  13. stringstream ss;
  14. T dst;
  15.  
  16. if ( !( ss << src ) )
  17. throw runtime_error( "CType: stringize error" );
  18.  
  19. if ( !( ss >> dst ) )
  20. throw runtime_error( "CType: invalid conversion" );
  21.  
  22. return dst;
  23. }
  24.  
  25. template <class T>
  26. T GetType( istream& is )
  27. {
  28. string s;
  29.  
  30. if ( !getline( is, s ) )
  31. throw runtime_error( "GetInt: input failure" );
  32.  
  33. return CType<T>( s );
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. using namespace std;
  40.  
  41. int age;
  42. string name;
  43.  
  44. // Create the problem as an example
  45. cout << "Please type your age: ";
  46. cin >> age;
  47. cout << "Please type your name: ";
  48. getline( cin, name );
  49. cout << name << " is " << age << " years old\n";
  50.  
  51. // Clean up the stream for an accurate test
  52. cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  53.  
  54. // Eliminate the problem with GetType
  55. cout << "Please type your age: ";
  56. age = Raye::GetType<int>( cin );
  57. cout << "Please type your name: ";
  58. getline( cin, name );
  59. cout << name << " is " << age << " years old\n";
  60.  
  61. return 0;
  62. }
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 >>?
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.
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.
Reply With Quote  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Rep Power: 2
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: It's cinfull!

  #18  
Jan 7th, 2007
Originally Posted by murschech View Post
So what's a poor programmer to do?
'Don't use 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.
Originally Posted by murschech View Post
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 >>?
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.
Originally Posted by murschech View Post
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?
Assume nothing and you'll never be disappointed.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
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

Other Threads in the C++ Forum

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