Help with a 2D array from a text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

Help with a 2D array from a text file

 
0
  #1
Nov 12th, 2006
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Nov 13th, 2006
  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.
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #3
Nov 13th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #4
Nov 13th, 2006
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.
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #5
Nov 13th, 2006
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #6
Nov 13th, 2006
Why not to use EOF see HERE
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #7
Nov 13th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC