Getline Skipping

Pages: 12
Hi guys,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
//declaração de strings
char nome[100];
int idade;
char morada[500];
char localidade[50];
char pais[50];

cout << "Bem-vindo ao registo de novo utilizador.\n";
cout << "Insira o seu nome: ";
cin.getline(nome,100);
cout << "Insira a sua idade: ";
cin >> idade;
cout << "Insira a sua morada: ";
cin.getline(morada,500);
cout << "Insira a sua localidade: ";
cin.getline(localidade,50);
cout << "Insira o seu pais: ";
cin.getline(pais,50);

cout << "\n\n";
//output

cout << "Chamas-te:" << nome << "\n";
cout << "Tens " << idade << " anos\n";
cout << "Moras na " << morada << "\n";
cout << "Vives em " << localidade << "\n";
cout << "Es de " << pais << "\n";
cout << endl;


system("PAUSE");
return 0;
}


When I cin the "idade" variable, I don't know why, but the getline of morada is skipping, and it cout "Insira a sua morada: Insira a sua localidade: " in just one line.

Any suggestions?

Regards
hi
i guess this problem occur becuz there is a ENTER character in the keyboard buffer and you can solve it using 2 successive cin like this :
cin.getline(morada,500);
cin.getline(morada,500);
i 'll certainly works !
that's because "idade" don't have the ability to keep the "enter" in it, so "enter" stay in the buffer and is tranfered to the next getline and skip it, since getline finishes when "enter" is called.

just clear the buffer after "cin >> idade;".
I have an idea: let's do it the right way.
The previous two posters are sort of correct: the input buffer is flushed on encountering LF (linefeed, or '\n'); but the linefeed stays in the buffer. So you must remove the newline:
1
2
std::cin >> var;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

That will take the newline, and anything else that we don't want, out of the buffer.

You will need to #include <limits> .
yea
chrisname 's way is the correct one and mine is the easiest and you can just write
cin.ignore(256,'\n');
before
cin.getline(morada,500);
in your code.
Um... problem... streamsize is a little bit larger than 256.
http://cplusplus.com/reference/iostream/streamsize/

Though I can't see anyone putting 256 characters into the stream, in the event that someone copied and pasted this post into cin...

-Albatross
Um... problem... streamsize is a little bit larger than 256.

Not to mention that std::cin.ignore() should be called after std::cin.getline(), otherwise you'll just ignore the input you want and get the stuff you don't...
Thanks guys! I used chrisname' solution, but thanks to everyone. But could you explain and does it really do? Well, as I'm starting, I would like to understand everything..

And how can I insert accents in C++?

Cheers
Chrisname's solution does the following:

cin.ignore takes two parameters. The first is a size limit, the second is a delimiting character. If either is reached, the readout stops just after the delimiting character or the last character of the size limit. The first argument is set to the largest possible size of the stream (which is obtainable by using std::numeric_limits<std::streamsize>::max()), the second is set to a newline. In short, this code keeps on reading and discarding characters until it has discarded them all or until it has reached a newline.

-Albatross

Cool, I understood now. How do you know that codes and the properties of each parameter available?

And could you please help me with my second question?

Thank you
Argh... internet flickered just as I was posting that.

http://web.cs.mun.ca/~michael/c/ascii-table.html
"\ascii-number"

These should help, although possibly you could just enter the characters straight and it would work. I wouldn't know because I've never tried.

-Albatross
Well I tried this:
cout << "Insira o seu pa\237s: ";

But just showed a strange sign instead of "í".
Hee hee... oops. That was an abridged table.
http://www.asciitable.com/

You probably wanted 161.

-Albatross
Well, it showed the character "q" ..
Ah... goodness... idiotic me...

The number is supposed to be octal. Sorry about that. Provided I messed up no calculations, it wanted 241 for an accented i. Sorry.

I need to get some coffee...

-Albatross
Yeah, no problem. Worked :)

Sorry, but where did you got that octal one? 'Cause I was needing the list to type another char.

Thanks again.
Damn, sorry, but I couldn't understand. And how could I do something like "É"?

Regards
http://www.ascii.cl/conversion.htm

Try this then. It's a table. Find the decimal value you want in the leftmost column and then find the octal equivalent in the next-to-rightmost column.

For É, use the extended table I gave you earlier, but note that the value for É (144) is decimal, and that you need to convert it to its octal equivalent.

Or you could use Unicode...

-Albatross
The table you gave me to me don't include all the characters, for example "ê"...

Do you know any other?

Sorry for bothering you.

Thanks
Pages: 12