0

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
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 2
    It would help if we knew what the contents of the names.txt file was.... – Jerry Jeremiah Jul 18 '16 at 07:21
  • it's a list of around 4429 lines each one starting with a name followed by 11 columns of int – Francisco Jul 18 '16 at 07:23
  • 1
    Are there spaces in the name? What is the delimiter? Please edit a couple of lines of the actual file being used into your question. – Retired Ninja Jul 18 '16 at 07:25
  • So those are all spaces and there is a blank line between each line of data? And then by definition the name cannot have spaces? – Jerry Jeremiah Jul 18 '16 at 07:29
  • Possible duplicate: http://stackoverflow.com/questions/29814933/read-data-from-file-and-store-into-an-array-of-structs – 0aslam0 Jul 18 '16 at 07:30
  • Yep, the blank spaces are included in the data and the names cannot have spaces. – Francisco Jul 18 '16 at 07:32
  • Also I'm bounded to use using namespace std; I know is a bad habit but my professor is really strict on us using it – Francisco Jul 18 '16 at 07:35
  • That's like somebody teaching you to never park facing uphill even when you have an automatic transmission. It's stupid advice and if they get paid o teach it to you then you should ask for your money back. – Jerry Jeremiah Jul 18 '16 at 07:48
  • If everything is separated by whitespace and there is no whitespace in any of the values and there are always the same number of values (1 name, 11 numbers) then just call cin 12 times in the loop instead of using getline. But since (in real life) any of those assumptions could change at any time then it might be good instead to use getline to read each string and then put it in a string stream and then use cin to read the values from the string stream. The hard part is the name - if it could contain spaces then you need to parse the string yourself... – Jerry Jeremiah Jul 18 '16 at 07:53
  • And what problems are you having with your program? What is your question? – Jim Mischel Jul 18 '16 at 14:22
  • I cannot pass the array to the function and then return it to the main function, which I need to use some other applications. Right now the program is reading the text file but not is not storing it in the array list. – Francisco Jul 18 '16 at 15:55

1 Answers1

0

So I was able to store the data from the file into the array but now I'm having trouble separating the data into names and numbers

  void loadNames( Name list[]){
  int count=0;
  int ch;
  char line[SIZE];
  int lineNumber[SIZE];

ifstream nameList;
nameList.open("names.txt");
do{
        {
            nameList.getline(line,SIZE);
            strcpy (list[count].name, line);
            }
        count++;
}while((ch=nameList.peek())!=EOF);

nameList.close();