Program ignoring getline function.

Started by
15 comments, last by Austin0v 15 years, 8 months ago
Quote:Original post by Austin0v
Quote:Original post by SiCrane
Well, the way you have it now you don't do any error detection and you're making an assumption discarding 256 characters will get you to the next line. For example, try feeding your program "Moo" as the number and watch what happens. Or 5 a space and then a few hundred 5's after that before you hit enter.


Just curious, what's the proper way to use the ignore function with proper error detection?


Don't. Instead, use the following "buffering" strategy.

Read each line of input into a string. That way you will never have to 'ignore' anything, since you always read everything that's currently available on std::cin.

Then, use the std::stringstream object to attempt to pull data out of the string (your "buffer"). If it doesn't work (because the input data was invalid), you can simply try again, and you don't have to reset anything. The next time through the loop, you get a brand new stringstream object, with the next line of user input.

You can simplify things for yourself a little bit by making a templated function to get the line, read in from it into a value, and return whether or not it was successful.

// input and output practice#include <iostream>#include <string>#include <sstream> // where std::stringstream lives.using namespace std;template <typename T>bool getValue(istream& is, T& result) {	string line;	getline(is, line);	return std::stringstream(line) >> result;}int main() {	int i;	string name;	do {		cout << "Please enter a number: ";	} while (!getValue(cin, i));		cout << "You entered the number " << i << endl;	// minor English lesson here :)	cout << "If you multiply that by 2 you get " << i*2 << endl;	        cout << "What is your name? ";	getline(cin, name); // the input data is always in string format ;)	cout << "\nhello " << name << " you're awesome!";}


Edit: Er, of course. Behold as I completely fail at being a ninja and editing it in. =)

[Edited by - Zahlman on August 19, 2008 9:49:59 PM]
Advertisement
Quote:Original post by Zahlman
	do {		cout << "Please enter a number: ";	} while (!getValue(i));

Minor bug fix:

	do {		cout << "Please enter a number: ";	} while (!getValue(std::cin, i));
Thank you so much for all your help. Now in my never ending quest to know every aspect of everything, why is it that the buffer isn't ignored in the first place without having to ignore it or without having to have a function to extract only the data you need? Is there a reason that all the data is stored in that buffer?
Basically, the formatted extraction operators and the getline() function represent two fundamentally different ways of interacting with a stream. The formatted extraction operators work by stripping out all whitespace and then processing what's left as the desired type. So two successive formatted extractions will behave like you would expect. Ex: this code will read and output two integers if the data is either "5\n6\n" or "5 6\n" or even "\t5 \t\n6\n".
  int i, j;  std::cin >> i;  std::cout << i << "\n";  std::cin >> j;  std::cout << j << "\n";

getline(), however, works by taking everything in the stream up to the next new line and dumping it into the buffer and ripping out the newline. So do you see the difference? Formatted extraction removes all whitespace before the extracted data. Whereas getline() removes only a newline after the extracted data.
Quote:Original post by SiCrane
Basically, the formatted extraction operators and the getline() function represent two fundamentally different ways of interacting with a stream. The formatted extraction operators work by stripping out all whitespace and then processing what's left as the desired type. So two successive formatted extractions will behave like you would expect. Ex: this code will read and output two integers if the data is either "5\n6\n" or "5 6\n" or even "\t5 \t\n6\n".
  int i, j;  std::cin >> i;  std::cout << i << "\n";  std::cin >> j;  std::cout << j << "\n";

getline(), however, works by taking everything in the stream up to the next new line and dumping it into the buffer and ripping out the newline. So do you see the difference? Formatted extraction removes all whitespace before the extracted data. Whereas getline() removes only a newline after the extracted data.


Ok, I understand now, what are some examples of practical uses of using one or the other?
You use the first when you're picking out individual items, and the second when you actually care about the lines.
You all are an amazing help, thank you so much.

This topic is closed to new replies.

Advertisement