943,950 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 861
  • C++ RSS
Apr 29th, 2008
0

String Input - Not Happening Second Time

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
DigitalPackrat is offline Offline
33 posts
since Jan 2008
Apr 29th, 2008
0

Re: String Input - Not Happening Second Time

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'.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 29th, 2008
0

Re: String Input - Not Happening Second Time

Great, added cin.clear() and cin.sync().

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

Thanks.
Reputation Points: 10
Solved Threads: 1
Light Poster
DigitalPackrat is offline Offline
33 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: saving bitmap file
Next Thread in C++ Forum Timeline: Accepting Characters Without Waiting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC