Tgolding
November 23rd, 2004, 12:26 AM
I am trying to write a piece of code that will check user input to a file and if the input matchesthe file, they can continue on (It is a login process). the code I have thus far is
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string>
#include <fstream>
using namespace std;
#define USERS 7
struct User_Struct
{
char User_Id[81];
char Password[81];
};
void Load_User_Table(User_Struct [], int);
void Main_Menu();
int main()
{
int row=0;
char inputuser[81],
inputpass[81];
User_Struct userid_pass[USERS];
cout << "\nPlease enter your user name: ";
cin.get (inputuser, 81);
cin.ignore (81, '\n');
cout << "\nPlease enter your password: ";
cin.get (inputpass, 81);
Load_User_Table(userid_pass, USERS);
if ((inputuser != userid_pass[row].User_Id) && (inputpass != userid_pass[row].Password))
Load_User_Table(userid_pass, USERS);
//cout << "\nUser name and password not found!" ;
else
Main_Menu();
//Display_User_Table(userid_pass, USERS);
cout << endl;
system("PAUSE");
return 0;
}
void Load_User_Table(User_Struct userid_pass[], int size)
{
int row=0;
fstream user_file("Users.txt", ios::in);
if (user_file.fail())
{
cout << "\nFile did not open!";
exit(1);
}
user_file.getline( (char *) &userid_pass[row].User_Id, sizeof(User_Struct), ',');
user_file.getline( (char *) &userid_pass[row].Password, sizeof(User_Struct));
while (!user_file.eof())
{
cout << "\n\n" << setw(20) << "User Name"
<< setw(20) << "Password"
<< endl;
cout << "\n" << setw(20) << userid_pass[row].User_Id
<< setw(20) << userid_pass[row].Password;
user_file.getline( (char *) &userid_pass[row].User_Id, sizeof(User_Struct), ',');
user_file.getline( (char *) &userid_pass[row].Password, sizeof(User_Struct));
}
user_file.close();
}
void Main_Menu()
{
cout << "\nThis is the main menu area!";
}
It is not complete yet but I am running into a few errors My first question is, do I need to use a strcmp to compare the user input to the file?
The second part is, when I run it, the output showing the contents of the file do not show the last line of the file, and it shows the first line of the file when I don't want it to. How can I correct this?
Thanks for any help in advance.
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string>
#include <fstream>
using namespace std;
#define USERS 7
struct User_Struct
{
char User_Id[81];
char Password[81];
};
void Load_User_Table(User_Struct [], int);
void Main_Menu();
int main()
{
int row=0;
char inputuser[81],
inputpass[81];
User_Struct userid_pass[USERS];
cout << "\nPlease enter your user name: ";
cin.get (inputuser, 81);
cin.ignore (81, '\n');
cout << "\nPlease enter your password: ";
cin.get (inputpass, 81);
Load_User_Table(userid_pass, USERS);
if ((inputuser != userid_pass[row].User_Id) && (inputpass != userid_pass[row].Password))
Load_User_Table(userid_pass, USERS);
//cout << "\nUser name and password not found!" ;
else
Main_Menu();
//Display_User_Table(userid_pass, USERS);
cout << endl;
system("PAUSE");
return 0;
}
void Load_User_Table(User_Struct userid_pass[], int size)
{
int row=0;
fstream user_file("Users.txt", ios::in);
if (user_file.fail())
{
cout << "\nFile did not open!";
exit(1);
}
user_file.getline( (char *) &userid_pass[row].User_Id, sizeof(User_Struct), ',');
user_file.getline( (char *) &userid_pass[row].Password, sizeof(User_Struct));
while (!user_file.eof())
{
cout << "\n\n" << setw(20) << "User Name"
<< setw(20) << "Password"
<< endl;
cout << "\n" << setw(20) << userid_pass[row].User_Id
<< setw(20) << userid_pass[row].Password;
user_file.getline( (char *) &userid_pass[row].User_Id, sizeof(User_Struct), ',');
user_file.getline( (char *) &userid_pass[row].Password, sizeof(User_Struct));
}
user_file.close();
}
void Main_Menu()
{
cout << "\nThis is the main menu area!";
}
It is not complete yet but I am running into a few errors My first question is, do I need to use a strcmp to compare the user input to the file?
The second part is, when I run it, the output showing the contents of the file do not show the last line of the file, and it shows the first line of the file when I don't want it to. How can I correct this?
Thanks for any help in advance.