943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4950
  • C++ RSS
Nov 12th, 2006
0

Help with a 2D array from a text file

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7.  
  8. //FONCTION LireInfo
  9. void LireInfo (char Noms [80][30])
  10. {
  11.  
  12. //Variables
  13. int x, y, Counter=0;
  14.  
  15. //Instructions
  16. ifstream fichier ("employe.txt");
  17. if (fichier.is_open())
  18. {
  19. while (! fichier.eof() )
  20. {
  21. for(y = 0; y < 80; y++) //#of employees could reach 80
  22. {
  23. for(x =0; x <30; x++)//30 is a max length for names
  24. {
  25. fichier.getline (Noms[x], 50, ':');
  26. //I try to use 30 instead of 50 to stop getline but it
  27. // doesn't work
  28. }
  29. }
  30. }
  31. fichier.close();
  32. }
  33. else
  34. cout << "Ooops"; //if file doesn't open properly
  35. for (y=0; y<80; y++) //loop to show the array
  36. {
  37. for (x =0; x <30; x++)
  38. {
  39. cout<<Noms[y][x];
  40. }
  41. cout<<endl;
  42. }
  43. }
  44.  
  45.  
  46.  
  47. //MAIN
  48. void main ()
  49.  
  50.  
  51. {
  52. //Variables
  53. char Noms [80][30];
  54.  
  55.  
  56. //Instructions
  57. LireInfo(Noms);
  58.  
  59.  
  60. }
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

C++ Syntax (Toggle Plain Text)
  1. for(y = 0; y < 80; y++) //#of employees could reach 80
  2. {
  3. for(x =0; x <30; x++)//30 is a max length for names
  4. {
  5. 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.
C++ Syntax (Toggle Plain Text)
  1. for(y = 0; y < 80; y++) //#of employees could reach 80
  2. {
  3. fichier.getline (Noms[y], 30, ':');

3. you need another getline() call inside that loop to read the mainder of the line.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

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.
  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream> // New addition. Needed for stringstream- WoLfPaCk
  5. #include<iomanip>
  6. using namespace std;
  7.  
  8. //FONCTION LireInfo
  9. void LireInfo (char Noms [80][30])
  10. {
  11. //Variables
  12. int x, y, Counter=0;
  13. //Instructions
  14. ifstream fichier ("employe.txt");
  15. if (fichier.is_open())
  16. {
  17. string token;
  18. stringstream ss;
  19. int line_count = 0;
  20. while (getline (fichier,token) && line_count < 80)
  21. {
  22. ss.str(token);
  23. ss.getline( Noms[ line_count ], 30, ':' );
  24. ++line_count;
  25. }
  26. fichier.close();
  27. }
  28. else
  29. cout << "Ooops"; //if file doesn't open properly
  30. for (y=0; y<80; y++) //loop to show the array
  31. {
  32. for (x =0; x <30; x++)
  33. {
  34. cout<<Noms[y][x];
  35. }
  36. cout<<endl;
  37. }
  38. }
  39.  
  40. //MAIN
  41. void main ()
  42. {
  43. //Variables
  44. char Noms [80][30] = { 0 }; // Remember to Initialize
  45. //Instructions
  46. LireInfo(Noms);
  47. }
Last edited by WolfPack; Nov 13th, 2006 at 12:31 am.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

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.
C++ Syntax (Toggle Plain Text)
  1. 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.
C++ Syntax (Toggle Plain Text)
  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7.  
  8. //FONCTION LireInfo
  9. int LireInfo (char Noms [80][30], int Heures [80], float Taux [80])
  10. {
  11.  
  12. //Variables
  13. int x, y=0, Compteur=0;
  14.  
  15. //Instructions
  16. ifstream fichier ("employe.txt");
  17. if (fichier.is_open())
  18. {
  19. while (! fichier.eof() )
  20. {
  21. fichier.getline (Noms[y], 50, ':');
  22. fichier >> Heures[y];
  23. fichier >> Taux[y];
  24. y++;
  25. Compteur++;
  26. }
  27. fichier.close();
  28. }
  29. else
  30. cout << "Ooops";
  31. for (y=0; y<Compteur; y++)
  32. {
  33. for(x=0; x<30; x++)
  34. {
  35. cout<<Noms[y][x];
  36. }
  37. cout<<Heures[y] <<" " <<Taux[y];
  38. }
  39. cout<<"\n\n";
  40. return Compteur;
  41. }
  42.  
  43.  
  44. //MAIN
  45. void main ()
  46.  
  47.  
  48. {
  49. //Variables
  50. char Noms [80][30]={0};
  51. int Heures [80]={0};
  52. float Taux [80]={0};
  53.  
  54.  
  55. //Instructions
  56. LireInfo(Noms, Heures, Taux);
Last edited by earlyriser; Nov 13th, 2006 at 2:51 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

Why not to use EOF see HERE
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 13th, 2006
0

Re: Help with a 2D array from a text file

Thanks for the eof tip. It seems main () is a thing to avoid too.
I will remember these things.
Last edited by earlyriser; Nov 13th, 2006 at 3:34 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: aggregation vs public inheritance?
Next Thread in C++ Forum Timeline: [C++/Win32] Plugin System Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC