Hello. I'm having problems filling an array from a text file. I cann't figure out how this works with getline. I can read read and store ALL the lines, but what I'm trying to do is to store ONLY the names of the employees (from the text file) to a two dimensional array: Noms[80][30].

Do you have any idea what I should do?

// reading a text file
#include <iostream>
#include <fstream>
#include<iomanip> 
using namespace std;


//FONCTION LireInfo
void LireInfo (char Noms [80][30])
{

//Variables
    int x, y, Counter=0;

//Instructions
    ifstream fichier ("employe.txt");
    if (fichier.is_open())
    {
        while (! fichier.eof() )
        {
            for(y = 0; y < 80; y++) //#of employees could reach 80
            {
                for(x =0; x <30; x++)//30 is a max length for names
                {
                fichier.getline (Noms[x], 50, ':');
                //I try to use 30 instead of 50 to stop getline but it
                               // doesn't work      
                }
            }
        }
        fichier.close();
    }
    else 
        cout << "Ooops"; //if file doesn't open properly
    for (y=0; y<80; y++) //loop to show the array
    {
        for (x =0; x <30; x++)
        {
            cout<<Noms[y][x];
        }
        cout<<endl;
    }
}



//MAIN 
void main () 


{
    //Variables
char Noms [80][30];
    
    
//Instructions
LireInfo(Noms);    


}

The text file is something like this:
Jean-Paul Toto : 30 10.50
Annie Lelong : 35 12.0
Sebastien Lebarbu : 20 8.75
Jules Lemince : 25 10.15
Benoit Legros : 18 12.0
Pierre Quiroul : 20 17.50
Annie Hilant : 35 11.0
Charles Antoine : 20 18.75
...
30 characters for names, 31-... characters for other data that is not important at this point.


And this is what I get in execution:

ean-Paul Toto
30 10.50
Annie Lelong
35 12.0
Sebastien Lebarbu
20 8.75
Jules Lemince
25 10.15
Benoit Legros
18 12.0
Pierre Quiroul
20 17.50
Annie Hilant
35 11.0
Charles Antoine
20 18.75
Jules CÚsar
25 9.15
Benoit Legros
18 12.0
Jean Lemire
35 10.50
Anna Laba
25 12.0
Fred Cailloux
18 8.85
Arthur Leriche
25 50.15
Harry Potter
10 1200.0
Pierre Trudeau
23 150.50
Stephanie Ladouceu
35 22.0
Therese Krispies
12 12.0 ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
Press any key to continue

Yes, I'm a student and it's my homework, but I have doing all the things I know and I cann't solve it, so I really need help.

Thanks.

Recommended Answers

All 6 Replies

for(y = 0; y < 80; y++) //#of employees could reach 80
            {
                for(x =0; x <30; x++)//30 is a max length for names
                {
                fichier.getline (Noms[x], 50, ':');

1. If 30 is the max length for names, why is getline() reading 50 characters?

2. The for( x = ... loop is not necessary. You only need the Y loop.

for(y = 0; y < 80; y++) //#of employees could reach 80
            {
                fichier.getline (Noms[y], 30, ':');

3. you need another getline() call inside that loop to read the mainder of the line.

What Dragon said. Since I did the corrections anyway, here they are.

  • Remember not to use eof to test if the file hasnt finished reading.
  • Remember to initialize arrays or any variable if you are not sure that you will be inputting values.
  • Because your delimiter is a character other than '\n', you will usually have to process 2 getlines. Refer the code.
// reading a text file
#include <iostream>
#include <fstream>
#include <sstream> // New addition. Needed for stringstream- WoLfPaCk
#include<iomanip> 
using namespace std;
 
//FONCTION LireInfo
void LireInfo (char Noms [80][30])
{
    //Variables
    int x, y, Counter=0;
    //Instructions
    ifstream fichier ("employe.txt");
    if (fichier.is_open())
    {
        string token;
        stringstream ss;
        int line_count = 0;
        while (getline (fichier,token) && line_count < 80)
        {
            ss.str(token);
            ss.getline( Noms[ line_count ], 30, ':' );
            ++line_count;
        }
        fichier.close();
    }
    else 
        cout << "Ooops"; //if file doesn't open properly
    for (y=0; y<80; y++) //loop to show the array
    {
        for (x =0; x <30; x++)
        {
            cout<<Noms[y][x];
        }
        cout<<endl;
    }
}
 
//MAIN 
void main () 
{
    //Variables
    char Noms [80][30] = { 0 }; // Remember to Initialize
    //Instructions
    LireInfo(Noms);    
}

Thanks for your replies Ancient Dragon and WolfPack.

I have modified the code with some of your suggestions and added other stuff from my homework, it works as required but I have the feeling something should be wrong. By the way, the script is supposed to read and store 3 arrays from the 3 columns in the text file and show them.

I added two other arrays and I initilized all of them. The first array is a 2D array that stores the employee names, the second stores their worked hours (int) and the last one stores their pay per hour (float).

x loop removed.
I didn't add a second getline because the third value (pay per hour) ends the line.
Why the use of eof is a bad option?
I still use 50 in the getline even if I only wnat to read the first 30 characters, I don't know why but if I set this lower to the max number of characters per line, nothing is showed in the execution screen. I would like to set it at 30.

fichier.getline (Noms[y], 50, ':');

I didn't include sstream because I'm not sure I'm allowed to use it.

Here is the code. I hope the french stuff is not a problem.

// reading a text file
#include <iostream>
#include <fstream>
#include<iomanip> 
using namespace std;


//FONCTION LireInfo
int LireInfo (char Noms [80][30], int Heures [80], float Taux [80])
{

//Variables
    int x, y=0, Compteur=0;

//Instructions
    ifstream fichier ("employe.txt");
    if (fichier.is_open())
    {
        while (! fichier.eof() )
        {
                fichier.getline (Noms[y], 50, ':');
                fichier >> Heures[y];
                fichier >> Taux[y];
                y++;
                Compteur++;
        }
        fichier.close();
    }
    else 
        cout << "Ooops"; 
    for (y=0; y<Compteur; y++)
    {
        for(x=0; x<30; x++)
        {
            cout<<Noms[y][x];
        }            
        cout<<Heures[y] <<" " <<Taux[y];
    }
    cout<<"\n\n";
    return Compteur;
}


//MAIN 
void main () 


{
    //Variables
    char Noms [80][30]={0};
    int Heures [80]={0};
    float Taux [80]={0};
    
    
//Instructions
LireInfo(Noms, Heures, Taux);

>>Why the use of eof is a bad option?
because it can cause the loop to run too many times. eof is not detected until there is an attempt to read. When the last byte is read from the file eof() has not yet been reached.

Why not to use EOF see HERE

Thanks for the eof tip. It seems main () is a thing to avoid too.
I will remember these things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.