username and password validation

i m trying to create a project on username and password validation but while executing it accepts even wrong username and password stored in the file
(i m using turbo c++ and i m a beginner too)



#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
char uname[20];
char pass[5];
void username();
void ucheck();
void main()
{ clrscr();
int a,b,c;
cout<<"1-existing user\n2-new user";
cin>>a;
if(a==1)
{
ucheck();
}
if(a==2)
{
username();
}
getch();
}
void username()
{
ofstream fi;
fi.open("username.txt",ios::out|ios::app);
cout<<"enter desired username";
gets(uname);
cout<<"enter desired password";
for(int i=0;i<5;i++)
{pass[i]=getch();
cout<<"*";
}
fi<<"\n"<<uname<<"\n";
fi.write((char*)&pass,sizeof(pass));
}
void ucheck()
{char ch,name2[20],pass2[5];
int found;
cout<<"enter username";
gets(name2);
cout<<"enter password";
for(int j=0;j<5;j++)
{pass2[j]=getch();
cout<<"*";}
ifstream fin;
fin.open("username.txt",ios::in) ;
fin.seekg(0);
while(!fin.eof()) {
fin.getline(uname,20);
fin.read((char*)&pass,sizeof(pass));
if(strcmp(name2,uname)==0&&strcmp(pass2,pass)==0)
found=1;
}
if(found==1)
cout<<"welcome";
else
cout<<"sorry";
fin.close();
}
It's not exactly a solution to your problem, but one thing that will help you, and the people you are asking for help, to understand your code is to use indenting. There are a variety of indenting schemes out there, but here's an example of how your code might look:

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
60
61
62
63
64
65
66
67
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
char uname[20];
char pass[5];
void username();
void ucheck();
void main()
{
    clrscr();
    int a,b,c;
    cout<<"1-existing user\n2-new user";
    cin>>a;
    if(a==1)
    {
        ucheck();
    }
    if(a==2)
    {
        username();
    }
    getch();
    }
        void username()
    {
        ofstream fi;
        fi.open("username.txt",ios::out|ios::app);
        cout<<"enter desired username";
        gets(uname);
        cout<<"enter desired password";
        for(int i=0;i<5;i++)
        {
            pass[i]=getch();
            cout<<"*";
        }
        fi<<"\n"<<uname<<"\n";
        fi.write((char*)&pass,sizeof(pass));
    }
    void ucheck()
    {
        char ch,name2[20],pass2[5];
        int found;
        cout<<"enter username";
        gets(name2);
        cout<<"enter password";
        for(int j=0;j<5;j++)
        {
            pass2[j]=getch();
            cout<<"*";
        }
        ifstream fin;
        fin.open("username.txt",ios::in) ;
        fin.seekg(0);
        while(!fin.eof())
        {
            fin.getline(uname,20);
            fin.read((char*)&pass,sizeof(pass));
            if(strcmp(name2,uname)==0&&strcmp(pass2,pass)==0)
                found=1;
        }
        if(found==1)
            cout<<"welcome";
        else
            cout<<"sorry";
        fin.close();
}


Now having taken a closer look at your code I notice that at least one thing you should do is initialize found to 0. Sometimes you will find that your data is zeroed for you, but you should never rely on this.

Also be wary of using sizeof(pass). This may not be giving 5 as you expect. It may be returning the size of pointers on your system. A simple test like cout<<sizeof(pass); should tell you.

I'm also noticing that strcmp is supposed to take c-strings, which are arrays of chars ending in 0 (note this is not the char '0'). Pass and pass2 do not have 0 at the end. However I would not expect this to result in a false positive.

I would suggest putting a cout statement before your while loop to double check the value of found and another one inside the if block to see if your strcmp is passing. Perhaps the one inside the if block could print out name2, uname, pass2 and pass to see in what conditions it's setting found to 1;
sir it is still not working.can u provide me the changed code?
The password strings, pass and pass2 are't large enough.

You're probably not reading the password correctly as far as I can tell, and you're definitely not comparing pass and pass2 correctly. Have you tried running this in Turbo Debugger to see what's going on?
May std::cin contains 2byte unicode characters. It happens in some 64bit systems. Check it.
Last edited on
Topic archived. No new replies allowed.