// 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!!

Edited 5 Years Ago by jimmymack: n/a

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????

There are some input forms that don't read the entire line. If you use one of these to read an integer, it reads the integer only. It stops reading after all digits have been read and leaves the rest. If you enter 54 23 12[ENTER] and read an integer, 23 12[ENTER] is left in the buffer. If next you want to read the person's name, the getline() will read 23 12[ENTER] as the name. That's why you must clear the buffer.

'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.

Why? Because it says so. That the way getline() is designed.

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.

Yes.

There are some input forms that don't read the entire line. If you use one of these to read an integer, it reads the integer only. It stops reading after all digits have been read and leaves the rest. If you enter 54 23 12[ENTER] and read an integer, 23 12[ENTER] is left in the buffer. If next you want to read the person's name, the getline() will read 23 12[ENTER] as the name. That's why you must clear the buffer.


Why? Because it says so. That the way getline() is designed.


Yes.

Thanks what you said makes sense but I'm still having trouble understanding this:

For example , if you read in an integer, (is this with the getline function or just with cin?) then a whole line (what is this at the same time? like this: 54 sheep were on the farm in Sommerset[ENTER] (or seperate?) 54 [ENTER] sheep were on the farm in Sommerset [ENTER] 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.

Thanks for your help. I think I understand it now not a 100 percent sure I do but think I do lol.

This article has been dead for over six months. Start a new discussion instead.