#include<conio.h>
#include<iostream.h>
class customer
{
char city[21];
char state[21];
char name[51];
char email[81];
char address[201];
public :
void getdetails();
void showdetails();
};
void customer::getdetails()
{
cout<<" Enter Your Full Name (Max 50 chars): ";
cin.getline(name,51);
cout<<"\n\n Enter e-mail (Max 80 chars): ";
cin.getline(email,81);
cout<<"\n\n Enter You Address (Max 200 chars): ";
cin.getline(address,201);
cout<<"\n\n Enter City (Max 20 chars ): ";
cin.getline(city,21);
cout<<"\n\n Enter State (Max 20 Chars): ";
cin.getline(state,21);
cout<<"\n\n\t\t\t\t Thank You ";
cout<<"\n\n\t\t\t PRESS ANY KEY TO CONTINUE ";
_getch();
}
void customer::showdetails()
{
cout<<"\nName : "; puts(name);
cout<<"E-mail : "; puts(email);
cout<<"Address : "; puts(address);
cout<<"City : "; puts(city);
cout<<"State : "; puts(state);
}
void main()
{
void accept();
void display();
clrscr();
int ch;
customer obj;
cout<<"1.Enter details "<<endl;
cout<<"2.Exit "<<endl;
cin>>ch;
while(ch==1)
{
if(ch==1)
{
clrscr();
obj.getdetails();
clrscr();
obj.showdetails();
}
cout<<"1.Enter details "<<endl;
cout<<"2.Exit "<<endl;
cin>>ch;
}
getch();
}
void accept()
{
clrscr();
customer obj;
obj.getdetails();
clrscr();
obj.showdetails();
}
After i compile and run this program and enter choice as 1 (to enter details) -
the program skips my name and directly goes to accepting my e-mail ! Its like i pressed enter when it asked me to enter my name even though i didn't.
After the cin statement, use:
cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');
to clear the stream of the carriage return, so the getline statement doesn't end right away.