| Zaita (2301) | |||
|
Too many questions keep popping up with the same problem. How do I get user input from cin using >> into X type. Using the >> operator opens you up to alot of problems. Just try entering some invalid data and see how it handles it. Cin is notorious at causing input issues because it doesn't remove the newline character from the stream or do type-checking. So anyone using cin >> var; and following it up with another cin >> stringtype; or getline(); will receive empty inputs. It's best practice to NOT MIX the different types of input methods from cin. I know it's going to be easier to use cin >> integer; than the code below. However, the code below is type-safe and if you enter something that isn't an integer it will handle it. The above code will simply go into an infinite loop and cause undefined behaviour in your application.Another dis-advantage of using cin >> stringvar; is that cin will do no checks for length, and it will break on a space. So you enter something that is more than 1 word, only the first word is going to be loaded. Leaving the space, and following word still in the input stream.A more elegant solution, and much easier to use is the getline(); function. The example below shows you how to load information, and convert it between types.
| |||
|
Last edited on
|
|||
| firedraco (5414) | |
| Well said. In fact, I have an idea...this post should be stickied (no that wasn't my idea). | |
|
|
|
| belkdaddy (15) | |
| is there a way for a user input information without it being displayed to the monitor, such as entering a passord? | |
|
|
|
| firedraco (5414) | |
| Without using OS specific stuff (or maybe ncurses, but I don't think it let you do that), no. Otherwise, go look it up for your OS of choice. | |
|
|
|
| Zaita (2301) | |
| http://www.cplusplus.com/forum/beginner/4402/ | |
|
Last edited on
|
|
| mindsaber (5) | |
|
You spelled "elegant" wrong. ;) | |
|
|
|
| Buff Dogg (13) | ||
Of course he spelled it wrong. Did you forget what kind of website this is? Programmers can't spell. | ||
|
|
||
| Zaita (2301) | |
|
Indeed I did. Unfortunately, New Zealand has a terrible accent and it makes it difficult to spell phonetically. "Elegant" is said as "elegent" so when touch-typing I often revert to a phonetic style of spelling. That is also why you will notice I spell worlds like Organisaton with an S, and not a Z. UK spelling. But as Buff mentioned. Most programmers are notoriously terrible at spelling. | |
|
|
|
| Duoas (6734) | |
|
> Most programmers are notoriously terrible at spelling. Oh, yeah! Vindication! I feel a lot better about being such a poor speller now! (I'm serious, too.) On *nix, there is often a convenient little function called getpass() http://linux.die.net/man/3/getpass On Windows, you will usually want to mess with the Credentials API. There is an excellent article on this subject here: http://msdn.microsoft.com/en-us/magazine/cc163883.aspx For a simple console application though... you can just turn off echo with SetConsoleMode(). Hope this helps. | |
|
|
|
| cppg2009 (14) | |
| Really nice way to validate user input .. Thanks ! | |
|
|
|
| cppg2009 (14) | |
|
I have question regarding to this: what is the difference between: cin.getline(buf,100); // where char buf[100] and using getline(cin,input); // Above code segment Let me know asap | |
|
|
|
| Bazzy (6281) | |
|
cin.getline is for C strings (character arrays) getline is for C++ strings | |
|
|
|
| Zaita (2301) | |
|
@cppg2009: The difference is that using a char array is a C style of handling the information. Your also limited to a maximum of 100 characters being loaded into your array. The string method allows an un-restricted number of characters to be loaded. You also avoid stupid issues like non null-terminated character arrays causing problems when being used. | |
|
|
|
| awing (3) | |
|
i'm having a problem when i run the program from the sample #include <iostream> #include <string> using namespace std; int main () { string mystr; cout << "What's your name? "; getline (cin, mystr); cout << "Hello " << mystr << ".\n"; cout << "What is your favorite team? "; getline (cin, mystr); cout << "I like " << mystr << " too!\n"; return 0; } i entered a full name(under variable mystr), then it was displayed like what it has to be in the line cout<<"hello"<<mystr<<".\n"; then i entered my favorite team but the 2nd value of mystr didn't displayed anymore, it only displayed "i like" then blank , i'd like to know why please reply thanks. | |
|
|
|
| seymore15074 (449) | |
| I vote for moving this to the Beginners forum and stickying it there. | |
|
|
|
| Zaita (2301) | |
@awing: That works fine for me. But if your having problems, before the 2nd getline(); try putting cin.clear();
| |
|
|
|
| awing (3) | |
| @ zaita: ok i'll try it! thanks a lot | |
|
Last edited on
|
|
| kikolani (26) | |||||
I think it would be much easier to get input from user using a much simplier code like
for numbers and
for a string. | |||||
|
Last edited on
|
|||||
| Scipio (442) | |
|
@kikolani no, that gives a lot problems. If you use cin>>int and the user enters "hallo" you're program will most likely crash or at least it won't do what it should do. And that's only one of the problems when using cin>>. In simple programs for school-assigments etc. you may use cin>> but in 'real' programs always use the solutions Zaita presented here when possible. | |
|
|
|
| guestgulkan (2831) | |||
|
I came to Zaita's conclusion some time ago too. IMO the cin stream is just to prone to user input errorand in a situation like this cin >> a >> b >> c, once one part fail everything following tend to fail. I started to wite some functions but as template functions:
Still Work in progress. I know you can't chain inputs this way, but I think it's safer. | |||
|
|
|||