| | |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






