I've encountered a problem with getline(). I've googled it and checked multiple pages, but those answers aren't meant for newbies like me. Here's the code:
#include <iostream> #include <string.h> using namespace std; int main(){ string n,s,a,ad,e; cout<<"Your name: "<<endl; cin>>n; cout<<"Hello, "<<n<<endl; cout<<"Your surname: "<<endl; cin>>s; cout<<"Your age: "<<endl; cin>>a; cout<<"Your address: "<<endl; getline(cin,ad); cout<<"Your email: "<<endl; cin>>e; cout<<"Done."<<endl; cout<<"Name: "<<n<<" "<<s<<endl; cout<<"Age: "<<a<<endl; cout<<"Address: "<<ad<<endl; cout<<"Email: "<<e<<endl; }
The problem is that the getline(cin,ad) just skips whenever I launch the program. It goes this way:Your name: name Hello, name Your surname: surname Your age: age Your address: Your email:
As you can see, I can't enter my address, because the getline() gets skipped. How can I fix this, simply?
I solved the problem, I just should've written:
cin>>ad;
getline(cin, ad);
and that's all. Thanks for your contribution!