fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. // player node for linkedlist
  6. class Player {
  7. public:
  8. int id;
  9. string name;
  10. string position;
  11. int age;
  12. float salary;
  13. Player* next;
  14. Player() : id(0), name(" "), position(" "), age(0), salary(0.0), next(NULL) {}
  15.  
  16. };
  17. // player linkedlist
  18. class team {
  19. public:
  20. int rank;
  21. string name;
  22. string president;
  23. Player* head;
  24. team() : rank(0), name(""), president(""), head(NULL) {}
  25.  
  26. // function to insert newnode(add new player)
  27. void addPlayer(int id, string name, string position, int age, float salary) {
  28. Player* newPlayer = new Player();
  29. newPlayer->id = id;
  30. newPlayer->name = name;
  31. newPlayer->position = position;
  32. newPlayer->age = age;
  33. newPlayer->salary = salary;
  34.  
  35. if (head == NULL) {
  36. head = newPlayer;
  37. } else {
  38. Player* temp = head;
  39. while (temp->next != NULL) {
  40. temp = temp->next;
  41. }
  42. temp->next = newPlayer;
  43. }
  44. }
  45.  
  46. void readTeamFromFile( string fileName) {
  47. ifstream inputFile(fileName);
  48.  
  49. if (!inputFile.is_open()) {
  50. cout << "Unable to open file "<< endl;
  51. return;
  52. }
  53.  
  54. string line;
  55. while (getline(inputFile, line)) {
  56. // Find positions of commas
  57. /*19,nada mohamed
  58.   pos1 -> 1->9->, =2;
  59.   pos2->1->9->,->n->a->d->a->space->m->o->h->a->m->e->d->,=15
  60.   */
  61. size_t pos1 = line.find(",");
  62. size_t pos2 = line.find(",", pos1 + 1);
  63. size_t pos3 = line.find(",", pos2 + 1);
  64. size_t pos4 = line.find(",", pos3 + 1);
  65. // Extract data from the line
  66. /*pos1+1 skip first comma 19|,n/ada
  67.   pos1=| & pos1+1=/ , pos2-pos1 find space between tow comma & -1 without second comma*/
  68. int id = stoi(line.substr(0, pos1));//stoi convert string into int num
  69. string name = line.substr(pos1 + 1, pos2 - pos1 - 1);
  70. string position = line.substr(pos2 + 1, pos3 - pos2 - 1);
  71. int age = stoi(line.substr(pos3 + 1, pos4 - pos3 - 1));
  72. float salary = stof(line.substr(pos4 + 1));
  73.  
  74. // Add the player to the linked list
  75. addPlayer(id, name, position, age, salary);
  76. }
  77.  
  78. inputFile.close();
  79. }
  80.  
  81. void displayPlayers() {
  82. Player* temp = head;
  83. if (temp==NULL) {
  84. cout << "No players in the team." << endl;
  85. return;
  86. }
  87.  
  88. while (temp) {
  89. cout << temp->id << "," << temp->name<< "," << temp->position << "," << temp->age << "," << temp->salary << endl;
  90. temp = temp->next;
  91. }
  92. }
  93.  
  94. };
  95.  
  96. int main() {
  97. team team1, team2, team3, team4, team5, team6, team7, team8, team9;
  98.  
  99. // Read and display data for each team manually without using a loop
  100. cout << "Team 1 (El Bank Al-ahly):******" << endl;
  101. team1.readTeamFromFile("El Bank Al-ahly.txt");
  102. team1.displayPlayers();
  103.  
  104.  
  105. cout << "Team 2 (Elgouna):*******" << endl;
  106. team2.readTeamFromFile("Elgouna.txt");
  107. team2.displayPlayers();
  108.  
  109.  
  110. cout << "Team 3 (Enppi):******" << endl;
  111. team3.readTeamFromFile("Enppi.txt");
  112. team3.displayPlayers();
  113.  
  114.  
  115. cout << "Team 4 (Ghazl elmahala):*****" << endl;
  116. team4.readTeamFromFile("Ghazl elmahala.txt");
  117. team4.displayPlayers();
  118.  
  119.  
  120. cout << "Team 5 (Haraas elhodod):******" << endl;
  121. team5.readTeamFromFile("Haraas elhodod.txt");
  122. team5.displayPlayers();
  123.  
  124.  
  125. cout << "Team 6 (ismaaily):*******" << endl;
  126. team6.readTeamFromFile("ismaaily.txt");
  127. team6.displayPlayers();
  128.  
  129.  
  130. cout << "Team 7 (Modern):********" << endl;
  131. team7.readTeamFromFile("Modern.txt");
  132. team7.displayPlayers();
  133.  
  134.  
  135. cout << "Team 8 (pharco):**********" << endl;
  136. team8.readTeamFromFile("pharco.txt");
  137. team8.displayPlayers();
  138.  
  139.  
  140. cout << "Team 9 (ZED):**********" << endl;
  141. team9.readTeamFromFile("ZED.txt");
  142. team9.displayPlayers();
  143.  
  144.  
  145. return 0;
  146. }
  147.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Team 1 (El Bank Al-ahly):******
Unable to open file 
No players in the team.
Team 2 (Elgouna):*******
Unable to open file 
No players in the team.
Team 3 (Enppi):******
Unable to open file 
No players in the team.
Team 4 (Ghazl elmahala):*****
Unable to open file 
No players in the team.
Team 5 (Haraas elhodod):******
Unable to open file 
No players in the team.
Team 6 (ismaaily):*******
Unable to open file 
No players in the team.
Team 7 (Modern):********
Unable to open file 
No players in the team.
Team 8 (pharco):**********
Unable to open file 
No players in the team.
Team 9 (ZED):**********
Unable to open file 
No players in the team.