Click to See Complete Forum and Search --> : Is a strcmp needed?


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.

Andreas Masur
November 23rd, 2004, 01:53 AM
Well...to make a long story short...it depends on the datatypes you want to compare...if you are using STL strings, then you simply can compare them...

string1 == string2

However, while dealing with old-style character arrays you have to use a comparison function such as 'strcmp()"...

Tgolding
November 23rd, 2004, 07:14 PM
Okay, I am going to assume that I need to use strcmp because I am trying to compare the info in an array to the info entered by a user. How would I go about doing this? Do I need to create a completely seperate function or can I do it from main with just a function used for loading the array?

Troy

PS- Sorry, I am just new to programming and the class was not exactly insightful and all the books I have browsed or have on hand have not really helped in trying to figure this out.

Andreas Masur
November 24th, 2004, 02:30 AM
Well...what are the datatypes you are trying to compare? Are they 'char*'?

HighCommander4
November 24th, 2004, 04:50 PM
If your strings are represented as char* arrays, you can compare them as follows

char* str1;
// initialize it to something
char* str2;
// initialize it to something
strcmp(str1, str2); // returns 0 if they are equal
// negative value if str1 precedes str2 alphabetically
// positive if str2 precedes str1 alphabetically

Do I need to create a completely seperate function or can I do it from main with just a function used for loading the array?

You can call strcmp() from any function.

Tgolding
November 24th, 2004, 07:15 PM
The strings are string1= the input from the user and string2= the lines located in the file. It is in Username/Password form. So the Username will have to equal the user name and the password will have to equal the corresponding password to the given username.