Last Program Help

I have to do this username, password, and PIN program, and i need help for the last part. I need to set up an array for 50 users and 3 attempts for 1 user. Here is what i have so far

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct UserRecord
{
string userID;
string password;
int PIN;

};

typedef UserRecord UserRecordArray [50];

UserRecord promptUser();
bool loginUser(UserRecordArray authorized_users, UserRecord user);
bool readUserFile(UserRecordArray users);
void printUsers(UserRecordArray users);


int main(void)
{

UserRecordArray userList;

readUserFile(userList);

printUsers(userList);

cout << endl << "Welcome to MyAmazingWebsite.com!" << endl << endl;

for(int attempt = 1; attempt <= 3; attempt++)
{
if(loginUser(userList, promptUser()))
{
attempt = 4; //breaks the for loop
}
else
{
cout << "Login failed. Please try again." << endl << endl;
}
}

return 0;
}

//Asks the user for their credentials and returns the corresponding UserRecord object.
UserRecord promptUser()
{
UserRecord user;
int pin;
cout << "Please enter your username, password, and PIN!" << endl;
cout << "Username: ";
cin >> user.userID;
cout << endl << "Password: ";
cin >> user.password;
cout << endl << "PIN: ";
cin >> pin;

while(typeid(pin) != typeid(int))
{
cout << endl << "Please enter an integer. PIN: ";
cin >> pin;
}
return user;
}

//Checks to see if user matches an object in authorized_users
bool loginUser(UserRecordArray authorized_users, UserRecord user)
{
//TODO
}

//passes in a UserRecordArray object and populates it from users.txt
//returns true on success, false on failure (use this to check the file read operations)
bool readUserFile(UserRecordArray users)

//loop through the users array and print each one
void printUsers(UserRecordArray users)


the comments are what i have to do, but I'm not sure how to do them
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
 
while(typeid(pin) != typeid(int))

That's not going to do what you want. That won't tell you if the user entered a non-numeric. pin is type int and the statement will never be false.

 
bool readUserFile(UserRecordArray users)

You're passing users by value. This means users is a local copy. readUserFile won't update the caller's array. You need to pass users by reference.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/


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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

struct UserRecord
{
    string userID;
    string password;
    int PIN;
    
};
typedef UserRecord UserRecordArray [50];

UserRecord promptUser();
bool loginUser(UserRecordArray authorized_users, UserRecord user);
bool readUserFile(UserRecordArray users);
void printUsers(UserRecordArray users);


int main(void)
{
	inFile.open(“user.txt”);
        if(inFile.open())
    	{
		while(inFile.good())
		{	
    
   			UserRecordArray userList;
    
   			readUserFile(userList);
    
    			printUsers(userList);
    	
    			cout << endl << "Welcome to MyAmazingWebsite.com!" << endl << endl;
    		
    			for(int attempt = 1; attempt <= 3; attempt++)
    			{
        			if(loginUser(userList, promptUser()))
        			{
            				attempt = 4;    //breaks the for loop
        			}
        			else
        			{
            				cout << "Login failed. Please try again." << endl << endl;
        			}
			}
		}
	}
    
    return 0;
}

//Asks the user for their credentials and returns the corresponding UserRecord object.
UserRecord promptUser()
{
    UserRecord user;
    int pin;
    cout << "Please enter your username, password, and PIN!" << endl;
    cout << "Username: ";
    cin >> user.userID;
    cout << endl << "Password: ";
    cin >> user.password;
    cout << endl << "PIN: ";
    cin >> pin;
    
    while(typeid(pin) != typeid(int))
    {
        cout << endl << "Please enter an integer. PIN: ";
        cin >> pin;
    }
    return user;
}
if(user.txt)
	{
	while(!user.txt.eof())
		{		
		getline(user.txt,userID,);
		getline(user.txt,password);
		getline(user.txt,PIN);
		cout << “Welcome!” << endl;
		}
	user.txt.close();
	}
bool loginUser(UserRecordArray authorized_users, UserRecord user)

if(!user.txt)
{
	cout << “Error opening output file” << endl;
	system(“pause”)
	return -1;
}


revised it, am i getting close?
There is also a file called user.txt incorporated into the program, how do i pass arguments as references with the text file?
Did you read what I wrote above?

 
  while(typeid(pin) != typeid(int)) 


That's not going to do what you want. That won't tell you if the user entered a non-numeric. typeid of pin is int and the statement will never be false.

Did you try to compile what you posted?

Line 24: infile is not defined.

Lines 75-85: Where do these lines belong? They're not part of a function.

Lines 86-93: Is this supposed to be a function? If so, it is incomplete. user is not defined. Missing opening and closing braces for the function.

how do i pass arguments as references with the text file?

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/





Topic archived. No new replies allowed.