This is a code i made, for checking if a nickname and password exists..
I have two question about this code...
1) In line 38 i close "fuser", but if i try to use this again there is an error. So, i used "fpass". Is there any way to use the same fot both files and not to create a new one?
2) When i check if the username exists, i get the line that the username is. Then i finde the password tha the user gives and i get the line if the pass exists. Finally i check if the number of these two lines are the same, so the user can loggin. But i thought that we can change it a little so, that it will have only one loop. What i thought is that we don't have the second while, but insteand an if, that checks if the password in the specific line that we got from the first while is the same with the one that the user gave at the beggining. Can we do this?
(I hope you understood what i said and what i ment :S )
#include <fstream>
#include <iostream>
#include <conio>
#include <string>
#pragma hdrstop
#pragma package(smart_init)
using namespace std;
void CheckLoginRequest(string username, string password);
main()
{
string user,pass;
cout<<"Give nickname";
cin>>user;
cout<<"Give password";
cin>>pass;
CheckLoginRequest(user,pass);
getch();
}
void CheckLoginRequest(string username, string password)
{
int linename=0,linepass=0;
string str;
int foundname=0,foundpass=0;
ifstream fusers("usernames.txt",ifstream::in);
while(getline(fusers,str) && foundname==0)
{
if (username==str)
{
foundname=1;
}
linename++;
}
fusers.close();
if(foundname==1)
{
ifstream fpass("passwords.txt",ifstream::in);
while(getline(fpass,str) && foundpass==0)
{
if (password==str)
{
foundpass=1;
}
linepass++;
}
fpass.close();
if(foundpass==1)
{
if(linepass==linename)
{
cout<<"you are now logged in"<<endl;
}
}
else
{
cout<<"wrong password,try again"<<endl;
}
}
else
{
cout<<"wrong username,try again"<<endl;
}
}