Click here to Skip to main content
15,885,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program to write in C++ that will generate files with the names of the same teachers, which are to be additionally sorted chronologically (by day and hour):

<hour> <day> <group> <surname> <subject>

(here is an example.txt content):

10:15-11:30 Friday gr1 Smith Programming
07:10-09:15 Wednesday gr2 Taylor InternetofThings
11:00-12:00 Monday gr2 Smith Java
10:20-11:45 Thursday gr1 Taylor Matchematic

and after working, program generate files:

Smith.txt :

11:00-12:00 Monday gr2 Java
10:15-11:30 Friday gr1 Programming

Taylor.txt :

07:10-09:15 Wednesday gr2 InternetofThings
10:20-11:45 Thursday gr1 Matchematic

What I've already managed to do is to load data from the txt file into the dynamic array (code below). I have no idea how to do a name search and sorting (names can be different and the number of lines can also be different). I can use stuff only from the standard library. Can't use map <> container.

What I have tried:

C++
struct Line {
  string hour;
  string day;
  string group;
  string surname;
  string subject; 
};

void readLine(ifstream& file, Line& line) {
  file >> line.hour >> line.day >> line.group >> line.surname >> line.subject;
}

void readLineTab(ifstream& file, Line* lineTab, const int numOfLines) {
  for (int i = 0; i < numOfLines; i++) {
    readLine(file, lineTab[i]); 
  }
}

void printLine(const Line& line) {
  cout << line.hour << " " << line.day << " " << line.group << " " << line.surname << " " << 
  line.subject << endl;
}

void printLineTab(Line* lineTab, const int size) {
  for (int i = 0; i < size; i++) {
    printLine(lineTab[i]);
  }
}

int checkFile(string& filePath, int& numOfLines) {
  ifstream file;
  file.open(filePath.c_str());
  if (file.fail()) {
    cerr << "Error file open: " << filePath << endl;
    file.close();
    return 1;
  }
  string line;
  int lineNr = 0;
  while (getline(file, line)) {
    lineNr++;
    numOfLines++;
  }
  file.close();
  return 0;
}

int main(int argc, char** argv) {
  int numOfLines = 0; 
  ifstream file; 
  string filePath = "example.txt"; 

  if (checkFile(filePath, numOfLines)) {
    return 1;
  }

  Line* lineTab = new Line[numOfLines];

  file.open(filePath.c_str());
  if (file.fail()) {
    cerr << "Error file open: " << filePath << endl;
    return 1;
  }

  readLineTab(file, lineTab, numOfLines);
  printLineTab(lineTab, numOfLines);

  delete[] lineTab;
  file.close();
  return 0;
}
Posted
Updated 20-Jan-21 3:04am
Comments
Richard MacCutchan 20-Jan-21 7:50am    
Think about the order that you need to do each step.
1. Create a list that will hold each element. A std::vector would be a good choice as it is dynamically expandable.
2. Read each line of text and build the structure of the separate fields. Add this to the list.
3. You can now pass the list to a method that prints each item, sort into a new list etc.

1 solution

The following code will possibly help
C++
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct Line
{
  string hour;
  string surname;
};

int cmp_line( const Line & la, const Line & lb)
{
  if ( la.surname < lb.surname )
    return true;
  else if ( la.surname > lb.surname )
    return false;

  return (la.hour < lb.hour);
}

int main()
{ 
  Line line[] =
  { 
    { "10:15-11:30", "Smith" },
    { "07:10-09:15", "Taylor" },
    { "11:00-12:00", "Smith" },
    { "10:20-11:45", "Taylor"},
  };
  
  size_t count = sizeof(line) / sizeof(line[0]);
  
  sort( begin(line), end(line), cmp_line);
  
  for (size_t n=0; n<count; ++n)
  { 
    cout << line[n].surname << " " << line[n].hour << endl;
  }
}
 
Share this answer
 
Comments
Maciej Los 20-Jan-21 9:41am    
5ed!
CPallini 20-Jan-21 13:51pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900