#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace 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.
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.
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:
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.
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..
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.
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.