Where was that program at? Oh I was going to put in the city and country too. After testing it out from the previous post, it looks like I was a bit mixed up myself lol. getlines do work following each other; it's interchanging cin and getline that causes input skips (more on that below).
For instance I added additional input prompts to the last one, 3 getlines and 1 cin to show how this works:
With a do-while loop, the program continues to prompt the user to continue and repeats if 'y' for yes is entered (think videogame ending menus). Mine is a bit simplified - the logic behind getting it to continue on 'y', quit on 'n' but repeat the continue statement for any other letter is a bit tricky to implement, so for now it only looks for a 'y' to continue, any other letter causes it to quit.
do{ [...] cin >> confirm [...] }while(confirm == 'y' || confirm == 'Y');
The program continues as long as the user enters y or Y for confirm because of the loop's check condition - it "does" the code inside the loop "while" the condition remains true.
The highlighted cin.ignore(); is particularly important - without this, the program skips any input of the prompt at the beginning of the loop. Upon returning to the start of this loop with the name prompt, it will read the '\n' inside the input buffer instead. When a letter is entered for confirm through cin, the new line from the [Enter] key is added to the end of the char input. When the loop returns to the beginning, getline() reads the new line still held as input and interprets it as the user pressing the [Enter] without giving any input. To the tester, it looks like the program skipped the user's turn to input their name and went straight to the city prompt, because the program still has the leftover newline in the buffer from the end of the loop.
cin >> confirm;
cin.ignore();
To fix this, cin.ignore(); should be un-commented. cin.ignore() can work with several parameters (for example, cin.ignore(5) "ignores 5 characters", or cin.ignore(12, 'u') to "ignore 12 characters or until character 'u' is found"). However the default behavior of cin.ignore() - with nothing in the parentheses - is to skip the next immediate character in the buffer. If the user enters 'y', 'y' is stored in confirm, and the '\n' from the [Enter] is ignored. Since 'y' makes the loop true, the loop returns to the beginning, and since the input buffer has nothing in it after the ignored newline, the program will correctly prompt the user with a getline() for their name again without getting the newline unintentionally.
With this added, the program will now continue to prompt three times and display the inputs until the user enters anything other than 'y' or 'Y' at "Continue".
output without ignore:
Enter your name: Enter your city: Los angeles
Enter your country: California
Hi! My name is .
I live in Los angeles, California.
Continue? (Y/N): n
output with ignore:
Enter your name: Suika-chan
Enter your city: a tree
Enter your country: near Hakurei Shrine
Hi! My name is Suika-chan.
I live in a tree, near Hakurei Shrine.
Continue? (Y/N): n
No comments:
Post a Comment