c++ - How to get a set of words with spaces as one input in C? -
in program created need customer information array. below codes regarding question.
struct customertype { string fname; string lname; char gender; string address; string contactno; }; customertype customer[1000];
i have following code input user. here i
index of customer i'm getting information about.
string add=""; cout<<left<<"\n"<<setw(29)<<"\t\t name"<<": "; cin>>customer[i].fname>>customer[i].lname; cout<<left<<"\n"<<setw(29)<<"\t\t gender"<<": "; cin>>customer[i].gender; cout<<left<<"\n"<<setw(29)<<"\t\t address"<<": "; getline(cin,add); customer[i].address=add; cout<<left<<"\n"<<setw(29)<<"\t\t contact no."<<": "; cin>>customer[i].contactno;
but when run program, asks input name, gender , contact no. not address. works there no getline
command.
how fix this?
this old problem if "getline
doens't skip newline in input, operator >>
does" problem. simple solutions include:
- use
cin.ignore(1000, '\n');
skip on next newline (assuming less 1000 characters before newline). line goes beforegetline
call. - use
getline
read data in general, , use other methods read out actual content. [in case, 1 difficultgender
member variable - want deal writing "female" , address becomes "emale" in way anyway, may not big issue.
Comments
Post a Comment