String Input - Not Happening Second Time

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 33
Reputation: DigitalPackrat is an unknown quantity at this point 
Solved Threads: 1
DigitalPackrat's Avatar
DigitalPackrat DigitalPackrat is offline Offline
Light Poster

String Input - Not Happening Second Time

 
0
  #1
Apr 29th, 2008
  1. class Customer {
  2.  
  3. private:
  4.  
  5. char name[50];
  6. int acc_no;
  7. char acc_type;
  8. float balance;
  9.  
  10. public:
  11.  
  12. void getData();
  13. };
  14.  
  15. int main() {
  16.  
  17. Customer cust;
  18. cust.getData();
  19. cust.getData();
  20. }
  21.  
  22. void Customer::getData() {
  23.  
  24. cout<<"Name : ";
  25. cin>>name;
  26.  
  27. cout<<"Acc No. : ";
  28. cin>>acc_no;
  29.  
  30. cout<<"Acc Type : ";
  31. cin>>acc_type;
  32. }

The second time I call the function getData of customer class, I does not ask me for the name. I have tried gets(), cin.getline. Again, the same problem.

Also, how can I prevent input of characters other than numbers in integers, float etc?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String Input - Not Happening Second Time

 
0
  #2
Apr 29th, 2008
At the end of getData() you need to add code to remove the '\n' from the keyboard buffer. Getting numbers (ints, floats, doubles) has the side affect of cin leaving the Enter key '\n' in the keyboard buffer, so the next time a character string needs to be entered cin appears not to do anything. Narue wrote a very good article about how to flush the input stream in this link.

>>Also, how can I prevent input of characters other than numbers in integers, float etc?
You have to accept them as strings and parse the characters to see if there are any non-numeric digits.

Another method is to call cin.peek() to see if there is anything in the keyboard except '\n'.
  1. int x;
  2. cout << "Enter a number ... ";
  3. cin >> x;
  4. if(cin.peek() != '\n')
  5. cout << "bad data\n";
Last edited by Ancient Dragon; Apr 29th, 2008 at 10:40 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 33
Reputation: DigitalPackrat is an unknown quantity at this point 
Solved Threads: 1
DigitalPackrat's Avatar
DigitalPackrat DigitalPackrat is offline Offline
Light Poster

Re: String Input - Not Happening Second Time

 
0
  #3
Apr 29th, 2008
Great, added cin.clear() and cin.sync().

>>When this works, it works beautifully.
(Narue's comments about cin.sync())
Thankfully it worked.

Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC