Program ignoring getline function.

Started by
15 comments, last by Austin0v 15 years, 8 months ago
I was able to get this program to recognize the getline by adding ignore but I don't understand why adding that works and why it doesn't work if you don't have it. Could someone please explain? Thank you!

// input and output practice

#include <iostream>
#include <string>
using namespace std;

#define SPACE "\n\n\n\n"

int main ()
{
	
	int i;
	string name;

	cout << "Please enter a number: ";
	cin >> i;
	cout << "You entered the number " << i << endl;
	cout << "If you times that by 2 you get " << i*2 << endl;
	

        // problem area
        cout << "What is your name? ";
	cin.ignore(256, '\n');
	getline(cin, name);
	cout << "\nhello " << name << " you're awesome!";


	cout << SPACE;
	system("pause");
	return(0);

}


Advertisement
When you use cin >> i it consumes the number characters in the stream and leaves the carriage return after the number. So if you call getline() immediately it consumes the carriage return and looks like it ignores the getline(). By adding the ignore you remove the carriage return from the stream before getline() is called.
Quote:Original post by SiCrane
When you use cin >> i it consumes the number characters in the stream and leaves the carriage return after the number. So if you call getline() immediately it consumes the carriage return and looks like it ignores the getline(). By adding the ignore you remove the carriage return from the stream before getline() is called.


Thanks, that makes some sense. But, now I have another question, is it improper practice to code that how I did?
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.
While we're on the topic of "poor practice": don't pause your programs artificially at the end (run it properly instead - from the command line or via a batch file), and use const identifiers for constants rather than #define hackery. Also, main() (as a special case!) doesn't require an explicit return value, and return values don't require parentheses around them (return is a keyword, not a function).
Quote:Original post by Zahlman
While we're on the topic of "poor practice": don't pause your programs artificially at the end (run it properly instead - from the command line or via a batch file)

Hope I'm not hijacking this thread by asking this, but why do people recommend against using system("pause")? When I write little test applications (generally they're less than 50 lines), I find it convenient to simply use system("pause") and then tell my IDE to run the program rather than creating a batch file or hunting down my new .exe in a command prompt. So what's wrong with system("pause") in a simple, little test application?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by MikeTacular
Hope I'm not hijacking this thread by asking this, but why do people recommend against using system("pause")?
It's non-standard and it's massively overkill to tell the operating system to pause your program rather than simply have the program wait for the user to press a key: std::cin.get();

You wouldn't want a 'real' program to halt at the end anyway - most all applications you use don't do that.

Quote:So what's wrong with system("pause") in a simple, little test application?
Nothing. Just keep in mind that it's both non-standard C++ and unprofessional in a polished project.
Quote:Original post by dmatter
Quote:Original post by MikeTacular
Hope I'm not hijacking this thread by asking this, but why do people recommend against using system("pause")?
It's non-standard and it's massively overkill to tell the operating system to pause your program rather than simply have the program wait for the user to press a key: std::cin.get();

Or just run the program properly from the IDE. In Visual Studio for example, use Run Without Debugging, Ctrl+F5.
Or stick a breakpoint on the last brace of main() and run it with the debugger.
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?

This topic is closed to new replies.

Advertisement