cin.getline help!
Nov 13, 2011 at 10:13am UTC
this is the code i wrote for a project (a part)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include<iostream.h>
#include<string.h>
#include<stdio.h>
char nm[10];
int i;
class plog
{
char name[30];
char rank[30];
char exp[50];
char dob[10];
char address[50];
public :
void create()
{
cout<<"\n---------------" ;
cout<<"\n Details" ;
cout<<"\n---------------" ;
cout<<"\n >Name\t" ;
cin>>name;
cout<<"\n >Rank" ;
cin>>rank;
cout<<"\n >Prev. experience\n" ;
cin.getline(exp,49,'.' );
cout<<"\n >Date of Birth" ;
cout<<"\n~\n" ;
cin>>dob;
cout<<" >Address" ;
cin.getline(address,49,'.' );
}
void disp()
{
cout<<"\n---------------" ;
cout<<"\n Details" ;
cout<<"\n---------------" ;
cout<<"\n >Name\n" ;
cout<<name;cout<<endl;
}
int comp()
{
if (strcmpi(nm,name)==0)
return 1;
else
return 0;
}
};
void main()
{
plog z;
z.create();
}
and this is the output.
---------------
Details
---------------
>Name XXXX
>Rank XXXXX
XXXX.
>Prev. experience
>Date of Birth
~
XXXXX.
XXXXX.
>AddressPress any key to continue
where the output should have been
---------------
Details
---------------
>Name XXXX
>Rank XXXXX
>Prev. experience
XXXXX.
>Date of Birth
~XXXXX.
>Address
XXXXX.
Press any key to continue
pls help.
guys its really really urgent
HELP MEEEEE!!!
(i work in Microsoft Visual C++ 6.0)
Last edited on Nov 14, 2011 at 7:34pm UTC
Nov 14, 2011 at 7:42pm UTC
cdeblast, your major prob is that you are trying to use both cin and cin.getline(). You can only use one or the other, not both. Only use cin.getline() otherwise you will end up with a broken program. And add a clear buffer fucntion
1 2 3 4 5 6 7 8
void clearbuf()
{
if (!cin)
while (cin.peek()!='\n' )
cin.ignore();
cin.ignore();
return ;
}
Feel free to use that exact code for the clearbuf.
Call clearbuf() after every cin.getline(). It will prevent buffer overflow into the next input line.
Topic archived. No new replies allowed.