| | |
String Input - Not Happening Second Time
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
C++ Syntax (Toggle Plain Text)
class Customer { private: char name[50]; int acc_no; char acc_type; float balance; public: void getData(); }; int main() { Customer cust; cust.getData(); cust.getData(); } void Customer::getData() { cout<<"Name : "; cin>>name; cout<<"Acc No. : "; cin>>acc_no; cout<<"Acc Type : "; cin>>acc_type; }
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?
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'.
>>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)
int x; cout << "Enter a number ... "; cin >> x; if(cin.peek() != '\n') 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.
![]() |
Similar Threads
- beginner in mips, stuck on program please help! (Assembly)
- Converting the array into string (C++)
- Not waiting for input during loop (C++)
- Tic Tac Toe help (Java)
- Form data & Back button (PHP)
- read each line of file (C)
- Scanning a text file for a string (C++)
- problem again (C)
- C++ Syntax (C++)
Other Threads in the C++ Forum
- Previous Thread: saving bitmap file
- Next Thread: Accepting Characters Without Waiting
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






