Student sorter

I am trying to sort a file containing data from 50 students and I am having loads of trouble. I know my plan, but cannot seem to get it into code for crap. My plan is something like:
1.) Open the file.
2.) Read in a line of text from the file (in main()), and save this into a string
variable.
3.) Set up 3 classes, Student Address and Date with Student being a parent of Address and Date.
4.) Pass this string variable into 3 classes by calling them dynamically, and calling their functions.
5.) In those functions, extract data up to a delimiter '|'. I have defined a function for this in Student.
6.) Since I want to sort by last name, I run a sort on the last name and an integer array at the same time.
7.) Somehow use the integer array as a guideline for rearranging the other arrays.
8.) Write the data back to the file, line per line, in the order it appeared in the file. Here's what I got so far (I am using separate files):
(main.cpp)
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
#include <iostream>
#include <fstream>
#include <string>
#include <new>
#include "Student.h"

using namespace std;

int main()
{   //This is an example version of the real deal. All we do here is test how we're going to pass the data from the datafile
    //to a string in main, and then that string should pass the data to the student class, which should extract the data it needs
    //and delete it from its received string. The remaining string is then passed to the other functions in student, and then,
    //in this degenerate example, it will be deleted for the string passing process to begin anew.
    string master;
    ifstream final("datafile.txt");
    Student*s = new Student[50];
    for (int row = 0; row<50; row++)
    {
        getline(final, master);
        s[row] = Student(row, master);
        s[row].getfirstnames();
        s[row].getlastnames();
    }   //end for
    return 0;
}   //end main 


(Student.cpp)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Student.h"
#include <iostream>
#include <sstream>
#include <string>
//#include <new>

using namespace std;

Student::Student()
{
    x = 0;
}   //end null constructor

Student::Student(int a, string b)
{
    x = a;
    tempo = b;
}   //end constructor

Student::~Student()
{
    //dtor
}

void Student::fname(size_t pos, int pos_three, string fnames[],char de)
{
    pos = 0;
    pos = Student::tempo.find(de);
    if (pos!=string::npos)  //If we find this | character...
    {
        pos_three = pos;  //...we store its position to an integer array,which is a guide for the selection of the last names.
    }   //end if
    string first_name= Student::tempo.substr (0, pos_three);  //Extracting the substring data...
    fnames[x] = first_name;     //...and now we store the extracted data to the fnames array for the bubble sort...
    //Student::first_array = Student::fnames[x];     //dumping the private array to a public one...
    Student::tempo.erase (0,pos_three+1);   //and erase those entries from the data array.
}   //end fname

void Student::setfirstnames()
{
    void fname(size_t temp, int pos_one, string firstnames[], '|');
    instructions[x] = x;    //put the integers in the guide array
}   //end setfirstnames

void Student::setlastnames()
{
    void fname(size_t temp, int pos_one, string lastnames[], '|');
}   //end setlastnames

void Student::setGPA() {
    void fname(size_t temp, int pos_one, string G_array[], '|');
    //dumping the contents of the string array to the float array...
    stringstream(G_array[x])>>GPAs[x];
}   //end setGPA

void Student::setcredits() {
    void fname(size_t temp, int pos_one, string cr_array[], '|');
    stringstream(cr_array[x])>>credits[x];
}   //end setcredits

string Student::getfirstnames()
{
    //Remember that this should return the values...
}   //end getfirstnames

string Student::getlastnames()
{

}   //end getlastnames

string Student::getGPA()

//Before we can start bubble sort, we must define the sswap() function:
string Student::sswap(string * first, string * second) {
    int temptwo;
    temptwo = *first;
    *first = *second;
    *second = temptwo;
}   //end sswap

string Student::mastersort(){
    //Let the bubble sort begin!
    string lnames[50],last_array[50];
    for (int b = 0; b < 50; b++)
    {
        for (int c = 0; c < 50; c++)
        {
            if (lnames[c]> lnames[c+1])
            {
                sswap(lnames[c], lnames[c+1]);
                //We might want to make an integer array that gets sorted, so that the instructions for the rearranging
                //of other arrays get created.
            }   //end if
        }   //end inner for
        //This string variable must accept 50 entries...
        last_array[b] = lnames[b];    //or we could dump the private array to a public array...
    }   //end outer for
}   //end mastersort 


(Student.h)
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
#ifndef STUDENT_H
#define STUDENT_H
#include <string>

class Student
{
    public:
        Student();
        Student(int,string);
        virtual ~Student();
        //There should be functions that have a string variable. The string variable will be the string passed to it by main.
        //Our universal getting method for use in this class and the other two...
        void fname(size_t, int, string[], char);
        string getfirstnames();    //These functions will extract what they need out of the string passed to them using string manipulation/substrings.
        string getlastnames();     //It may be passed to just one function and the one function will return the rest to the other functions. This may be an option.
        string getGPA();
        string getcredits();
        void setfirstnames();
        void setlastnames();
        void setGPA();
        void setcredits();
    protected:
        string tempo;
        size_t temp;
        int pos_one;
    private:
        string firstnames[50];      //All values obtained via the functions will be passed to these arrays to be sorted simultaneously by the bubble sort function. This way, we can return the person's
        string lastnames[50];       //...data at once.
        string G_array[50], cr_array[50];
        int instructions[50];
        float GPAs[50];
        int credits[50];
        string tempstr;
        int x;
        //We set the sort function private because there is no need for the other classes/main function to know what is going on inside it.
        string sswap(string *, string *);
        string * first, * second;
        string mastersort();
        //int GPA, credit;
};
#endif // STUDENT_H 


Any help would be greatly appreciated!
Last edited on
Why isn't anyone helping on this?
Last edited on
Can someone at least diagnose why the compiler is telling me something like "no matching function for call to getline" when I test out this extraction algorithm:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    size_t temp;
    string students, firstname[50],lnames[50];
    int pos_one[50], pos_two[50];
    ifstream myfile ("datafile.txt");   //Opens the data file.
    for (int r = 0; r < 50; r++)
    {
        getline(myfile, students);  
        temp = students.find('|');
        if (temp!=string::npos)  //If we find this | character...
        {
            pos_one[r] = temp;  //...we store its position to the first integer array...
        }   //end if
        getline(students,firstname[r],'|');
        cout<<"pos_one["<<r<<"] = "<<pos_one[r]<<'\t'<<"Student "<<r<<"'s first name is: "<<firstname[r]<<endl;
    }   //end for
    return 0;
}    //end main 


Please note that I am planning to delete the set of characters obtained by getline from the string students when I am done, using pos_one[] and pos_two[], but I took those out of the code to simplify things.
Last edited on
Why isn't anyone helping on this?
What's your problem?

Can someone at least diagnose why the compiler is telling me something like "no matching function for call to getline" when I test out this extraction algorithm:
Yes, getline() expects a stream (like 'myfile') and not the string 'students' (look at line 15)
I have the problem fixed! By the way, I attempted to make a makefile (which I needed to do anyways since I have a multifile program.) I am using Code::Blocks and the gcc compiler to run the program, but it is continuously failing to compile. I am also a newbie when it comes to make files. I hope I wrote this sorter.mak file correctly:

1
2
3
4
5
6
7
8
9
10
11

#my attempt at a makefile

sorter: Student.o main.o
    g++ Student.o main.o -o sorter

Student.o: Student.cpp Student.h
    g++ -c Student.cpp

main.o: main.cpp Student.h
    g++ -c main.cpp
I was literally taught by Andy Harris' links and this page: http://wiki.codeblocks.org/index.php?title=Code::Blocks_and_Makefiles

If someone could help with the makefile, they would have helped me with two projects in one post! Oh, I am running this using Windows.

What the hell am I doing wrong?
Last edited on
I've also modded my code a little bit (the final version is shown above), brought it all into one file, and I notice it complaining on the lines I was trying to use fname(). It does, for my three uses of the function, say something like:

expected identifier before '|'
expected ',' or '...' before '|'


It also says that it expects initializer before the 'string' (on the line where I define sswap()), when I've used namespace std the entire time! It also complains about there not being a matching call to the sswap function! What is going on?!
Last edited on
closed account (S6k9GNh0)
I'd rather use jam than make. I'd rather use cmake than jam. I'd rather there be a more organized and complete build system out there :/
Last edited on
Can you show me an example of this? I am such a newbie when it comes to doing stuff involving multiple files. (Notice, in my last post, I put it all in one file and tried to run THAT instead.) You have to keep that in mind. Not just you, but every other guy who is, at least, decent in C++ and knows more about this than people like me.

Also realize that I have been asking about this project (on here, and EVERYONE I knew), and diligently working on it/trying to look up stuff, MONTHS ago. It is past time to get it in. Luckily, the teacher is still accepting it. I have already told you the logic of what I think should go on, and I am trying NOT to get my grade murdered by this one stupid project. Now, could someone please help me with the problems I have stated above??
Last edited on
Topic archived. No new replies allowed.