Jump to content
  • Advertisement
Sign in to follow this  
Austin0v

Program ignoring getline function.

This topic is 3652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!