passing text from file and arranging in ascending order

Hi,

I have some text in this file. data.txt ( i need to make courseName run in ascending order)

maths abcd1244 A 3
english abcd1255 B 3
spanish abcd1222 D 3
physics abcd1111 C 3

how do i read it and cout them as:

physics abcd1111 C 3
spanish abcd1222 D 3
maths abcd1244 A 3
english abcd1255 B 3

This is all I am thinking,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class course
{
        string courseName, courseID;
        int credits;
        char grades;
}

void course::setCourse (string cName, string CID, int credit, char grade)
{
        courseName = cName;
        courseID = CID;
        credits = credit;
        grades = grade;

        /*need to do some sort of formula to do ascending, im thinking abcd is default, 
so just extract numbers out of courseName, and put numbers into array.*/
}

int main()
{
          ifstream input;
          string readFromFile;
          
          cout << "enter a filename:";
          cin >> readFromFile;
          input.open(readFromFile.c_str());
    
          if (!input.good())
          {
             cout << "File not found" << endl;
             system("pause");
             return 0;
          }

          for (int j = 0; j < 5; j++)
          {
            string courseName, courseID;
            int credits;
            char grades;
            
            input >> courseName >> courseID 
                  >> credits    >> grades;

            cout << setCourse (courseName, courseID, credits, grades);
           }


hope someone can get me thinking. Thanks. =D
Last edited on
Read the file, placing each line into a record, then each recorcd into a container.
Sort the container by the course name field.
Print the container.
Thanks kbw for your reply. Very comforting as you're the only one around to render the help. :)
There's loads of help around.

Have you made any progress?
Hmm, the OPs proposed output order looks wrong.

Course is a class so you have to instantiate an object. If you want to sort them you have to have many objects.
1
2
3
4
std::deque<course> CourseArray;
course tempObject;
tempObject.setCourse(courseName, courseId, credits, grades);
CourseArray.push_back(tempObject);


If you are up for it, you could compile and dissect the example here with your debugger and gain some insight into how to use std containers and algorithms for building dynamic arrays and sorting.
http://cplusplus.com/forum/articles/10879/

Good luck.
Man, forgot all about this thread as i was busy trying other methods and studying for quiz. Thanks for your help thou. :)
Topic archived. No new replies allowed.