By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

here i can't input address, after getting salary it takes input hire_date, what shou

P: 1
void getdata()
cout<<"SALARY = ";
cout<<"ADDRESS = ";
cout<<"HIRE DATE = ";
cout<<"PHONE = ";
cout<<"DATE OF BIRTH = ";
Dec 3 '16 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
What's this for?

Expand|Select|Wrap|Line Numbers
  1. ignore(cin,100);

Parsing input is a really advanced programming skill because you have no idea what the user will enter or when the data entry is finished.

Add to this the problem of cin >> data where an error occurs, like trying to put an A into an int. This causes the input stream to go into a fail state. Every subsequent cin will check the fail state and if it is turned on the cin fails. The effect is that on the first error it looks like every cin in the rest of the program is disabled since they all fail. Worse, when this happens you can't stop it.

For beginners, I suggest you enter exactly what your progrsm requires and nothing else. And, I would do this after you have completely tested your code by using hardcoded values. This way you can stay focused on the data entry parse and not be tripped up by logic errors in the rest of the program.

Just to cin >> to an int you need code like:

Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int* data)
  2. {
  3.     cin >> *data;
  4.     if (cin.good())
  5.     {
  6.       return true;
  7.     }
  8.     else
  9.     {
  10.         cin.clear();
  11.         return false;
  12.     }
  13. }
Even here you can see that you can't flush an input buffer because a)you don't know if the user has finished entering data, and b) your program is suspended while the user enters data. That means the input buffer is not yours so you can't flush it. All you can do is clear the error flag.

The only action you can take is if the error flag is set you can display a message and then terminate your program.

---unless you write your own version of cin which is what actual developers do.
Dec 3 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.