// quote.cpp - Script 4.6
// We need the iostream file in order
// to use out and cin.
#include <iostream>
// We need the string file
// For the string functionality
#include <string>
// Start the main function.
int main() {
// Declare the necessary variables.
std::string quote, speaker;
// Prompt the user for the quotation.
std::cout << "Enter a quotation (without quotation marks):\n";
std::getline(std::cin, quote);
// No extraneous input to be discarded
// because all input is assigned to the string!
// Prompt the user for the quotation's author.
std::cout << "Enter the person to whom this quote is attributed:\n";
std::getline(std::cin, speaker);
// Create a blank line in the output.
std::cout << "\n";
// Repeat the input back to the user.
std::cout << "The following quote has been received...\n\n"
<< quote << "n-" << speaker << "\n\n";
// Wait for the user to press Enter or Return.
std::cout << "Press Enter or Return to continue. \n";
std::cin.get();
// Return the value 0 to indicate no problems.
return 0;
} // End of the main function.
I don't understand the following text in my book and was wandering if anyone could help me understand it by breaking it down into the simpleist terms:
Because using getline() as in this example terminates the input read once it hits a newline (by default), 'you have to make sure no extraneous input exists in the buffer prior to calling it'.????? For example, if you read in an integer, then a whole line, the user may enter 54 followed by the Return. The 54 would be assigned to the integer variable, 'leaving Return in the buffer'????'This Return would immediately terminate the next getline()call'why????
'The getline() function discards the newline character that terminates the line.' Why does it do that???? So this newline will not be part of the string that recieves the entered value.
The getline() function takes an optional third argument that defines the stop character. By default getline() stops reading in when it hits a newline, but you could have it terminate be a # or whatever.
Any help to help me understand these three small passages with be greatly appreciated!!