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();
}
Last edited on
I have several pointers for you:
1. Stop using Turbo C++ - it's old, obsolete, and not up-to-date and it let you go away with this terrible example that should not even compile according to official C++ standard.
2. Download Microsoft Visual Studio C++ Express or Code::Blocks instead (both for free)
3. Read good tutorial, but not as old and obsolete as your IDE. I believe you are learning from a very old tutorial or book. Look at www.learncpp.com - they have some begginer friendly tutorials, that take into account recent changes to the C++ standard
4. Please, paste formatted code. there is this
<>
icon right to the edit field.
5. Please use spaces. This looks horrible:
if(strcmp(name2,uname)==0&&strcmp(pass2,pass)==0)
6. Look at my changes to your code - it's still buggy, but at least gets the job done.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;  // now you need sth like this

char uname[20];
char pass[5];
void username();
void ucheck();

int main()   // main is int
{
	//clrscr();
	int a,b,c;
	cout<<"1-existing user\n2-new user: \n";
	cin>>a;
	if(a==1)
	{
		ucheck();
	}
	if(a==2)
	{
		username();
	}

	//system("pause");
}
void username()
{
	ofstream fi;
	fi.open("username.txt",ios::out|ios::app);

	// Does the file even exist?
	if( !fi.is_open() )
	{
		cout << "Error opening file";
		exit(-1);
	}

	cout<<"enter desired username: ";
	cin.clear();              // entirely different way
	cin.ignore();             // of taking input,
	cin.getline(uname, 20);   // not elegant but works
	cout<<"enter desired password: ";

	for(int i=0; i<5; i++)
	{
		pass[i]=getch();
		cout<<"*";
	}
	cout << "\nThank you";
	cin.get();
	fi << uname << "\n"; // removed "\n"
	fi.write((char*)&pass,sizeof(pass));
}
void ucheck()
 {
    char ch,name2[20],pass2[6];
    int found = 0; // Initialize this!
    cout<<"enter username: ";
    cin.clear();
    cin.ignore();
    cin.getline(name2, 20);
    cout<<"enter password: ";
    for(int j=0;j<5;j++)
    {
        pass2[j]=getch();
        cout<<"*";
    }
    pass2[5] = 0;
    ifstream fin;
    fin.open("username.txt",ios::in) ;
    fin.seekg(0);
    fin.getline(uname,20);
    if(strcmp(name2,uname)!=0)
    {
        cout << "\nNo such user\n"; 
        return; // Added just for my convienience
    }
    while(!fin.eof())
    {
        fin.read((char*)&pass,sizeof(pass));
        if(strcmp(pass2,pass)==0)
            found=1;
    }
    if(found==1)
        cout<<"\nwelcome\n";
    else
        cout<<"\nsorry\n";
    fin.close();
 }
Last edited on
sir it is still showing welcome for invalid username and password in turbo c++
but it is working perfectly in borland c++.sir the problem is that we have to use turbo c++ compulsorily.can u help me sir??plz help me to code in turco c too.thank u.
Last edited on
Sorry for that, I edited it.
Do not make duplicate posts.

http://www.cplusplus.com/forum/general/90782/
Topic archived. No new replies allowed.