0
  1. How do I display leftover characters after a whitespace-delimited-input? Or, if that's incorrectly asked, how do I know that they're hiding there and need to be ignored?

  2. What must I do to avoid using the cin.ignore()? I was trying to learn how to use get() and getline(). It seems they only work if I first use cin.ignore(), which seems kind of inefficient to me.

My code prompts me for a number, and looks much like this:

int previousVar;
std::cout<<"\n\tWhat value should previousVar be?:\t";
std::cin>>previousVar;

Immediately afterwards, I added this code (which is copy-pasted from the cpp reference website):

std::string name;
//std::cin.ignore();
std::cout << "Please, enter your full name: ";
std::getline (cin, name);
std::cout << "Hello, " << name << "!\n";

cin>>beforeExit;

My code skips past the name-gathering section entirely to my end-of-program pause, unless I have the cin.ignore(). I read on one of the cpp reference pages that I may have leftover characters from a previous cin input. I saw a similar questions at http://www.cplusplus.com/forum/beginner/111268/ and also at difference between cin.get() and cin.getline() , but I'm not sure they're asking the same thing as I am.

kaya3
  • 47,440
  • 4
  • 68
  • 97
BrennickC
  • 101
  • 1
  • The only way to "avoid" `ignore` in your case, is if you implement what the `ignore` function does: Read characters one by one in a loop until you reach e.g. newline. Better to have a call to `ignore` which already does what you want. – Some programmer dude Nov 10 '16 at 05:22
  • @Someprogrammerdude So then 'ignore()' may be necessary any time I use 'getline()' after a 'cin'? Would some sort of 'cout' or something with an 'endl' work instead? It's not that I want to get around using 'ignore()', it's that I want to understand what's happening with the code, so I know why I'm using it. – BrennickC Nov 10 '16 at 05:47
  • The `cin` and `cout` objects are different objects with their own semantics and more importantly their own buffers. The only connection between `cin` and `cout` is the "tie" that causes `cout` to be flushed before doing any input on `cin`. – Some programmer dude Nov 10 '16 at 06:25
  • As for how `ignore` works, it's basically the way I told in my first comment: It loops, reading character by character from the input stream, until the end condition is met (it has read the specified number of characters, or it has read the terminator character). All the characters it reads it throws away, it *ignores* them. The function is needed because the terminal sends all characters it reads from the keyboard to your program, including newline for the enter key, and newline is what `getline` by default looks for. – Some programmer dude Nov 10 '16 at 06:27

1 Answers1

0

std::cin>>previousVar will not extract the characters after the int it reads, which includes the newline character. So if you don't use std::cin.ignore, std::getline (cin, name) will immediately end due to this newline character.

To avoid std::cin.ignore, you can either use std::getline in the first read, which will extract the newline character:

std::string previousVarStr;
std::cout << "\n\tWhat value should previousVar be?:\t";
std::cin >> previousVarStr;
auto previousVar = std::stoi(previousVarStr); // or (in C++17) use std::from_chars if you care about the performance

or use >> in the second read, which will ignore leading whitespaces:

std::string name;
std::cout << "Please, enter your full name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!\n";
xskxzr
  • 12,442
  • 12
  • 37
  • 77