How to read a line with Spaces in c++ ?

I want to store the address of a customer (with spaces) in a char variable (say cadd). First I tried to use "cin", as we know it reads until it sees any whitespace. So it reads only first word before a white space. So, I used "getline()" function, somebody suggested me it will work. But when I used it, It did'nt wait for the I/P (it skipped it). Whats the problem can anybody tell.

char cadd[20];

std::cout<<"Enter Customer Address:\n";
std::cin>>cadd;

std::cout<<"Enter Customer Address:\n";
std::cin.getline(cadd,20);
Thanks for the link MiiNiPaa.
The C language's scanf and fgets have this issue too. Finally I'm clear about it now.
You need to add cin.clear() and cin.ignore() before using another getline () or cin .
Thanks MiiNiiPaa for this helpful link.
Last edited on
Topic archived. No new replies allowed.