Kindly Check the Code Given Below and add comments where necessary and also Use of Hungarian Notation where its necessary......
Code is for User Login
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
string readString(ifstream&);
int main(int argc, char *argv[])
{
char UserID[80];
char Pass[80];
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string UserID_Pass[100][2];
fp_in.open("Users.txt");
if(fp_in == NULL)
{
cout << "Could not open user file, exiting."<<"\n";
return -1;
}
while(!fp_in.eof())
{
line_in=readString(fp_in);
if(line_in.length() > 0)
{
pos = line_in.find("\t",0);
if( pos != string::npos)
{
UserID_Pass[count][0] = line_in.substr(0,pos);
UserID_Pass[count][1] = line_in.substr(pos+1);
count++;
}
}
}
fp_in.close();
if(!count > 0)
{
cout << "User List is Empty, exiting" <<"\n";
return -2;
}
int logged_in=0;
while(!logged_in)
{
cout << "Enter User ID: ";
cin >> UserID;
cout << "Enter Password: ";
cin >> Pass;
int found = 0;
for(int x=0;x<count && !found;x++)
{
if(UserID_Pass[x][0].compare(UserID) == 0)
{
found = 1;
if(UserID_Pass[x][1].compare(Pass) == 0)
{
found = 1;
logged_in = 1;
}
else
{
cout << "Invalid Password!" <<"\n";
}
}
}
if(!found)
{
cout << "Invalid Username!" <<"\n";
}
}
cout << "Logged in Successfully!" <<"\n";
return 0;
}
string readString(ifstream& reader)
{
char buffer[256];
reader.getline(buffer,256);
string str1 = buffer;
return str1;
}
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std; //If you using a majority of the std namespace, there's really a point in using this.
string readString(ifstream& reader)
{
char buffer[256];
reader.getline(buffer,256);
string str1 = buffer;
return str1;
}
int main(int argc, char *argv[])
{
char UserID[80];
char Pass[80]; //You have strings! Use C++ or C based strings!
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string UserID_Pass[100][2];
fp_in.open("Users.txt");
if(fp_in == NULL)
{
cout << "Could not open user file, exiting."<<"\n";
return -1;
}
while(!fp_in.eof())
{
line_in=readString(fp_in);
if(line_in.length() > 0)
{
pos = line_in.find("\t",0);
if( pos != string::npos)
{
UserID_Pass[count][0] = line_in.substr(0,pos);
UserID_Pass[count][1] = line_in.substr(pos+1);
count++;
}
}
}
fp_in.close();
if(!count > 0)
{
cout << "User List is Empty, exiting" <<"\n";
return -2;
}
int logged_in=0;
while(!logged_in)
{
cout << "Enter User ID: ";
cin >> UserID;
cout << "Enter Password: ";
cin >> Pass;
int found = 0;
for(int x=0;x<count && !found;x++)
{
if(UserID_Pass[x][0].compare(UserID) == 0)
{
found = 1;
if(UserID_Pass[x][1].compare(Pass) == 0)
{
found = 1;
logged_in = 1;
}
else
{
cout << "Invalid Password!" <<"\n";
}
}
}
if(!found)
{
cout << "Invalid Username!" <<"\n";
}
}
cout << "Logged in Successfully!" <<"\n";
return 0;
}
|
NOTE: Just posting code and I'll add as I see errors and wronging in the code!
Last edited on