Public Group

# Program ignoring getline function.

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

## 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 on other sites
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 on other sites
Quote:
 Original post by SiCraneWhen 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 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 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 on other sites
Quote:
 Original post by ZahlmanWhile 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 on other sites
Quote:
 Original post by MikeTacularHope 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 on other sites
Quote:
Original post by dmatter
Quote:
 Original post by MikeTacularHope 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 on other sites
Or stick a breakpoint on the last brace of main() and run it with the debugger.

##### Share on other sites
Quote:
 Original post by SiCraneWell, 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?

• 40
• 15
• 10
• 23
• 10