#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int a, b, c;
cin >> a; cin >> b; cin >> c;
string s;
getline(cin, s);
return 0;
}
is there a newline hanging around cin's input buffer
for getline to accept and return immediately?
Because that is how the programming is behaving.
Yes.
However, 'getline' consumes a newline.
Cheers & hth.,
- Alf
Btw, what's wrong with "cin >> a >> b >> c;"?
> #include <iostream>
> #include <string>
> using namespace std;
> int main(void)
> {
> int a, b, c;
> cin >> a; cin >> b; cin >> c;
> string s;
> getline(cin, s);
> return 0;
> }
> is there a newline hanging around cin's input buffer
> for getline to accept and return immediately?
Probably. If you're reading from a console, most systems won't
pass the program anything until there is a new line, so the code
can't read the value for c until a new line is entered following
it. And of course, reading an int doesn't remove that new line.
The function getline doesn't necessarily look for a new line of
text; it can't know that you've already read characters from the
current line, so it simply reads from where you are up to (and
including) the next new line character.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Is there a preferred/standard way of discarding cin's input buffer?
Seems I want cin.ignore(). Thanks for the responses!