I am working on reading data from a file into a struct array. The first line of the file tells how many users there are, then the following lines are the username, password, and a pincode.So the file is setup as follows:
15
mah123
happy
1234
bah123
moody
4567
...etc
My array needs to be able to handle a maximum of 50 users. I successfully read in the number of users and the first username, password, and pincode but then it stops reading.
Here's my code
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int MAX_USERS = 50; //constants
int count;
ifstream inFile;
inFile.open("users.txt");
while(!inFile) //loop to notify user of invalid file
{
cout << "Error: File not found. " << endl; // invalid entry occurred
// message is printed
return 0;
}
struct UserLogin //struct to store data
{
string userName;
string passWord;
int userPin;
int currentUsers;
};
UserLogin loginInfo[MAX_USERS];
for (count = 0; count < MAX_USERS; count++) // loop to read data from file
{
inFile >> loginInfo[0].currentUsers;
inFile >> loginInfo[count].userName;
inFile >> loginInfo[count].passWord;
inFile >> loginInfo[count].userPin;
}
cout << loginInfo[0].currentUsers << setw(5) << loginInfo[1].userName << setw(10) << loginInfo[1].passWord << setw(6) << loginInfo[1].userPin << endl;
return 0;
}
The last line is being used to test to print out the data when i get the data to read in correctly.