now, how can i get it to make sure that they havent entered any spaces so the program doesnt crash down? htanx again everyone for answering all my questions!Code:char[20] pname; cin >> pname;
now, how can i get it to make sure that they havent entered any spaces so the program doesnt crash down? htanx again everyone for answering all my questions!Code:char[20] pname; cin >> pname;
In order of learned:
HTML - Mastered
CSS - Enough to use
SSI - Mastered
PHP - Advanced
C/C++ - Current Project
cin is delimited by white space, so if the user does enter a space then cin will read up to it, but not include it. If you want to read the spaces as well then you need to use getline:
char pname[20];
cin.getline ( pname, 20 );
-Prelude
My best code is written with the delete key.
Be aware that funny things can happen when you mix calls to istream::operator >>()and istream::getline(). getline extracts the delim char from the stream whereas operator >> leaves the delim in the stream and disregards it next read. So if you do this for instance....
It will appear that the getline has been skipped but in actual fact all that happened was getline found the delim char left by operator >> and took that as its input. This is easy to fix...Code:int a; char b[20] cin>>a; cin.getline(b,20);
Call this a preemptive strike.Code:int a; char b[20] cin>>a; cin.ignore(80,'\n'); // ignore up to 80 chars or a newline whichever comes first cin.getline(b,20); // will now work ok
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
As Stoned_Coder said...
The best way is to use ignore(), I had this problem once, and the best way is to use ignore();
none...
cin is a predefined instance of the istream class. As such it has a number of member functions and overloaded operators, two of which are >> and getline()---there are others like get(), ignore(), good(), bad(), fail(), clear(), etc.. A good textbook, or a decent compiler help section can help you learn about them. The most commonly used ones and the problems/quirks associated with them have been described already.