![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 56
| getline() and cin.ignore() |
| swappo is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Nov 2008
Posts: 56
| Example: Code: char loop = 'y';
while(loop != 'n')
{
cout << "Enter a name: ";
getline(cin, name);
cout << "Enter a number: ";
getline(cin, num);
numbers[name] = num;
cout << "Enter another name and number? 'y' or 'n': ";
cin >> loop;
}
Code: Enter a name: me Enter a number: 45 Enter another name and number? 'y' or 'n': y Enter a name: Enter a number: |
| swappo is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| You would use cin.ignore() after this line to ignore as many characters as necessary until the newline character: Code: cin >> loop;
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #5 |
| Registered User Join Date: Nov 2008
Posts: 56
| If I put cin.ignore() after getline(cin, name); I have a line with just a cursor which I have to hit enter to get to the next line of input. Yet, I put cin.ignore() after cin >> loop; as you suggested it works without that extra line. I don't understand why I get differing results with the different placements of cin.ignore(). |
| swappo is offline | |
| | #6 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #7 |
| Registered User Join Date: Nov 2008
Posts: 56
| Ok, thanks. |
| swappo is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Console window waiting on cin.ignore() or cin.ignore(2) | The SharK | C++ Programming | 3 | 07-19-2006 04:17 PM |
| getline problem | Nick_365 | C++ Programming | 1 | 03-10-2006 08:36 PM |
| getline problem | Bitphire | C++ Programming | 5 | 10-18-2004 04:42 PM |
| need help with cin.get, or cin.ignore | yoyo | C++ Programming | 5 | 09-23-2003 01:14 AM |
| getline run-on | twiz | C++ Programming | 4 | 03-02-2003 06:59 PM |