![]() |
| ||
| It's cinfull! In using "cin" for getting input I have about a 99% success rate. In the remaining 1% of cases the program does not pause for input. What causes this and how does one fix it? In ancient times when we programmed in C rather than C++ the same problem occurred occasionally if "getc" or "getchar" was used for input. You could solve the problem by putting the line "flushall()" before the input statement but that doesn't seem to work with cin. I ought to give an example but the only ones I have are in longish programs that I wouldn't ask anyone to read. |
| ||
| Re: It's cinfull! same problem as in old days. when you enter a number and press <Enter> cin does not remove the "\n" from the keyboard -- you have to do that int x; |
| ||
| Re: It's cinfull! Quote:
Quote:
#include <iostream>The best fix is to avoid the problem completely by not mixing formatted and unformatted input. You always use unformatted input and then include logic to do the formatting. With C we did it using fgets and sscanf or something like that. With C++ you do it with getline and stringstream or something comparable. #include <iostream>It is more work at first but the result is more robust. :) |
| ||
| Re: It's cinfull! Quote:
Though it would be really better if cin.clear( ) was placed before the call to cin.ignore( ) to deal with corrupted input stream along with the stray characters or junk in it. (eg. user enters a float in place of character). |
| ||
| Re: It's cinfull! Keep in mind, cin is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't! ;) |
| ||
| Re: It's cinfull! Yes true, but if already used, why not make it a bit more er... robust...:D |
| ||
| Re: It's cinfull! Quote:
Don't you mean >> operator is like c's scanf() ? cin by itself is similar to c's stdin file pointer. |
| ||
| Re: It's cinfull! Quote:
Well, in real code I would check the error state of cin and deal with legitimate problems long before trying to do cin.ignore(). But real code is harder to read and has lots of extras that hide the point I'm trying to make. :) Quote:
Not to be nitpicky, but cin is the C++ version of C's stdin. The overloaded >> operator is the C++ version of scanf. |
| ||
| Re: It's cinfull! Quote:
Yes, you guys are right. So I change my statement: Keep in mind, cin << is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't! ;) |
| ||
| Re: It's cinfull! Quote:
Quote:
cin >>, you mean. ;) |
| All times are GMT -4. The time now is 6:44 pm. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC