After line 16 add cin.ignore(); . This is beacuse you are mixing inputs and this will clear the '/n' that is left in the buffer after the call to cin with >> . Not 100% sure how gets() works but this fixes the problem when trying to use getline()
NathanOliver
Posting Virtuoso
1,524 posts since Apr 2009
Reputation Points: 281
Solved Threads: 280
Skill Endorsements: 3
Input in C++ is buffered. That means that when you type stuff on the keyboard it is stored in an internal "buffer----think array or queue" until input is terminated, usually by entering a new line char by hitting the enter key. The input waits in the buffer until an input function (method) access the input buffer. cin >> , gets() and getline() are all examples of input functions (methods). >> terminates input into the object on the right hand side of >> when it finds the first whitespace char (space char, new line char, tab, etc) and it leaves the terminating char in the input buffer. gets() terminates input into the object enclosed in the () when it identifies a new line char or the EOF marker in the input buffer. getline() terminates input into the associated string when the delimiting char is encountered and it removes the terminating char from the input buffer. (The default delimiting char for getline() is the new line char).
In your program you call cin >> before you call gets(). cin >> leaves the terminating new line char in the input buffer. When you call gets() the first thing it sees is the new line char in the input buffer left by preceding call to cin >>. It therefore thinks you don't want to put anything into whatever is in the () so whatever it is is left empty, or looks like it is skipped. To deal with this problem either call the ignore() function using appropriate syntax to ignore (remove) just the first char in the input buffer before you call gets() OR don't mix input methods like cin >> and gets() or cin >> and getline(), etc, in the same program.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
1. The problem you have is gets catches the EOL you get after hitting return for the number of the kids. Adding something like getch() before that gets() should fix this. Or just use gets() twice.
Using gets() at all is dangerous -- here's why. And gets() "does not catch" (whatever that means) the EOL. getch() is a completely non-portable solution and exists in very few compilers. It's best to learn proper techniques because learning to use gets() and getch() will cause major problems when you start programming for real.
2. Use string instead of string.h - the second one is legacy AFAIK.
No, string.h is the header used for c-strings. string is the header used for C++ strings.
3. Don't mix C++ and C I/O. Use istream::getline() or getline() from the string library.
True. If using getline in one place, why use gets() somewhere else?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Question Answered as of 1 Year Ago by
jaskij,
Lerner,
WaltP
and 1 other