Home > OS >  Forging multiple name lists from a text file into one
Forging multiple name lists from a text file into one

Time:11-07

I need to get a list of names from a txt file, and then sort them in alphabetical order. But let's just focus first on getting the list itself..

This is the input txt file (the format is given by the exercise) (comments are explanation given by the exercise, they're not actually there)

3 // the number of total name groups

5 // the number of names in the individual group
Ambrus Anna
Bartok Hanna Boglar
Berkeczi Aron
Kovacs Zoltan David
Sukosd Mate

7 
Biro Daniel
Csoregi Norbert
Drig Eduard
Dulf Henrietta
Fazekas Gergo
Gere Edit
Pandi Aliz

6
Albert Nagy Henrietta
Benedek Andor
Gere Andor
Lupas Monika
Pulbere David
Sallai Mark

So, I'm trying to get all 3 of the individual name groups and put them in a single array.. Here is the code:

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

void input(const char* fname, int& n, char students[100][100])
{
    ifstream file(fname);

    int groups, studNum; 
    char temp[50], emptyline[50];

    file >> groups;

    for(int i = 0; i < groups; i  )
    {
        file >> studNum;
        file.getline(emptyline, 100); //I actually don't know why there is an empty line after the numbers
    
        for(int j = 0; j <= studNum; j  )
        {
            //I'm going line by line with getline.. I'm not using fin, because sometimes the name consists of 3 elements, sometimes of 2
            file.getline(temp, 100); 
            strcat(students[j   n], temp);   
        }      
     
        n  = studNum;  
    }
    

    file.close(); 
}

int main()
{
    int n = 0;
    char students[100][100];
    input("aigi4153_L2_6.txt", n, students);

    //printing the array
    for(int i = 0; i < n; i  )
    {
        cout << students [i] << endl;
    }
    
    return 0;
}

So, the code looks good, and it almost works.. The output is 99% great, but there is a mysterious "6" before the name "Pulbere David".. And I have no idea how that goes there.. I don't think it has to do anything with the "6" before "Albert Nagy Henrietta", because if I change it to "7" for example, the mysterious "6" will remain the same number.. So, output is like this:

Ambrus Anna
Bartok Hanna Boglar
Berkeczi Aron
Kovacs Zoltan David
Sukosd Mate
Biro Daniel
Csoregi Norbert
Drig Eduard
Dulf Henrietta
Fazekas Gergo
Gere Edit
Pandi Aliz
Albert Nagy Henrietta
Benedek Andor
Gere Andor
Lupas Monika
6Pulbere David //here is the "mysterious 6"
Sallai Mark

Any ideas on how that 6 got there?

CodePudding user response:

You're reading the 6 as part of the names, your innermost loop should be for(int j = 0; j < studNum; j ) and not for(int j = 0; j <= studNum; j ) Also, you never initialize the contents of the students array, so strcat will try to append the name to a string that possibly contains anything. You should zero out the contents:

    char students[100][100];
    memset(students, 0, sizeof(students));
  • Related