I was working on a project in c++,which involved managing accounts.Here is an extract of source code of project
1.logging in
2.Changing password
You don't have to read the whole of the first code, because the problem occurs within the first few lines.As you might have guessed, user is the name of a class.
Now, here is the most weird problem I have ever faced-
1.If I directly call the directly,login( ) function it executes properly.OK very good.
2.If I call the login() function from change_password () function(by running it normally using ctrl+F9) it shows divide error exception.OK maybe something to do with the arguments passed.
3.If I execute the same function, through the same change_password () function, step by step,it shows General Protection Fault(not divide error exception)!!!
That's not all!!When I execute the change_password () function normally ,it fails to clear the screen itself ( the very first statement).Howe ver if I execute the same function step by step it get struck just before array of object is initialised( 3rd statement). Like I already mentioned if I directly call the login() function it does not show any error.Hence it has gone nothing to do with the array size.
Someone please explain this weird behaviour to me and correct my code.I appreciate an early response.
1.logging in
Code:
user user::login(short int pur)
{
clrscr();
fstream f("users.dat",ios::in|ios::out|ios::binary);
user u[10];
int i=0;
while(f.read((char*)&u[i],sizeof(user)))
i++;
f.close();
cout<<"Enter user name\n";
char n[30];
gets(n);
int pos=search(u,n,i);
int chk=0;
if(pos==-1)
{
cout<<"Sorry user not found\n";
return fail();
}
cout<<"Enter password\n";
gets(n);
int ver=verify_password(u[pos],n);
if(ver==0)
{
if(pur==0)
{
cout<<"Access granted\n";
getch();
}
return u[pos];
}
cout<<"Incorrect password\n";
return fail();
}
Code:
user user::change_password()
{
user u;
u=login();
.
. //The problem occurs within this.Hence,
. //I don't think the rest of the code
. //is required
.
Now, here is the most weird problem I have ever faced-
1.If I directly call the directly,login( ) function it executes properly.OK very good.
2.If I call the login() function from change_password () function(by running it normally using ctrl+F9) it shows divide error exception.OK maybe something to do with the arguments passed.
3.If I execute the same function, through the same change_password () function, step by step,it shows General Protection Fault(not divide error exception)!!!
That's not all!!When I execute the change_password () function normally ,it fails to clear the screen itself ( the very first statement).Howe ver if I execute the same function step by step it get struck just before array of object is initialised( 3rd statement). Like I already mentioned if I directly call the login() function it does not show any error.Hence it has gone nothing to do with the array size.
Someone please explain this weird behaviour to me and correct my code.I appreciate an early response.
Comment