I need some help in creating a function that will read data from a txt file into an array of structures. I am having trouble trying to store the data in the array list. The function loadNames, should be called to read the entire names.txt file and store the data from each line into a struct of type Name. The main function should pass an array of Name structs to loadNames so that it can simply read a line's data into the struct at the first element of this array, then read the second line's data into the second element, etc. This should be done for all 4429 lines of the names.txt file. Once loadNames has completed and returned to main, the file, names.txt, must never be read again during this execution of the application.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE=4429;
const int NAME_LEN=21;
const int NUM_RANKS=11;
//Structure used to store the data contained on one line of the file.
//name is an array of strings.
//rank is an array of int storing
struct Name{
char name[21];
int rank[11];
};
void loadNames(Name []);
int main(){
Name list[SIZE];
char choice;
loadNames(list);
return 0;
}
//The function that has been kicking my ass I tried using a loop
//to populate the array but I'm unable to separate the strings and the numbers
void loadNames( Name list[]){
ifstream nameList;
int count=0;
char line[4430];
int ch;
nameList.open("names.txt");
while((ch=nameList.peek())!=EOF){
nameList.getline(line,SIZE); // I was trying a [for] loop but I am
// not sure if it should replace the [while] loop.
};
nameList.close();
}
the txt file is as follows, (it's is longer but it follows the same format
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958