0

I'm trying to sort the students by first name, grade, and id. When I run it, it gives me the error "identifier not found." and I'm kind of confused because I have the void sortsID in the struct before the main. This is in my main which includes my myDate.h. I only included some of the code that I thought was important in this question

main.cpp

struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);

};

int main() {

studentData myClass[10];
myClass[0].name = "blah blah";
//adding names to class

    //random id numbers, bday, grade for each student
for (int i = 0; i < 10; i++) {
    int randomId = (rand() % (9999 - 1000 + 1)) + 1000;
    int randomGrade = (rand() % (100 - 50 + 1)) + 50;
    int randomMonth = rand() % (12 - 1) + 1;
    int randomDay = rand() % (31 - 1) + 1;
    int randomYear = rand() % (1994 - 1990) + 1990;
    myDate randomBday(randomMonth, randomDay, randomYear);
    myClass[i].id = randomId;
    myClass[i].grade = randomGrade;
    myClass[i].birthday = randomBday;
}

studentData *idSort[10];


    case 2:
        cout << "Sorting by ID" << endl;
        sortID(myClass, 10);
        cout << "Displaying original list";
        cout << "Student Info:\n";
        cout << "=================================================\n";
        cout << "NAME               ID#          GRADE   BDAY\n";
        for (int i = 0; i < 10; i++) {
            idSort[i] = &(myClass[i]);
            cout << left << setw(18) << idSort[i]->name << " ";
            cout << left << setw(12) << idSort[i]->id << " ";
            cout << setw(11) << setprecision(4) << idSort[i]->grade << " ";
            idSort[i]->birthday.display();
            cout << " " << endl;
        }
        break;


system("PAUSE"); };


void sortID(studentData s[], int n) {
studentData temp;   // Local variable used to swap records

for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number,
    swap the records
        if (s[i].id > s[i + 1].id)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}
void sortGrade(studentData s[], int n) {
studentData temp;   // Local variable used to swap records

for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number, swap the records
        if (s[i].grade > s[i + 1].grade)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}

void sort_on_name(studentData s[], int n) {
studentData temp;   // Local variable used to swap records

for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].name is later in alphabet than s[i+1].name, swap the two records
        if (strcmp(s[i].name, s[i + 1].name) > 0)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}

`

Jake
  • 313
  • 1
  • 7
  • 16
  • `sortID` is a member function. You need to call it on an object. – jrok Mar 11 '14 at 07:52
  • You might also want to not use bubble sort. – riklund Mar 11 '14 at 07:54
  • Besides needing to call it on the object as said by @jrok, it also should be *defined* as a member function and not a free-standing function. I think you need to [read a good book or tutorial on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Mar 11 '14 at 07:56
  • Correction. You declared a member function `sortID` inside `studentData`, but then you never define it. Later after `main`, you defined free function `sortID`, but that one is not visible before and from within `main`. Symbols need to be declared before they are used. Now, make up your mind, do you want member function or free function? – jrok Mar 11 '14 at 07:56
  • I take it this is for academia, and using `std::sort` with some functors is out of the question. =( bummer. – WhozCraig Mar 11 '14 at 07:59

2 Answers2

0

You declared function sortID as a non-static member function of class studentData

struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);

};

But you call it as a non-member function

sortID(myClass, 10);

Of course the compiler does not know how this name is declared because you declared name

studentData::sortID

Moreover the function defined after main with name sortID differs from the member function because its first parameter has type studentData s[] while the early declared function has the first parameter of type studentData

void sortID(studentData s[], int n) {
studentData temp;   // Local variable used to swap records

for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number,
    swap the records
        if (s[i].id > s[i + 1].id)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Thank you guys for all your help!
I just used the #include < algorithmn >

So I just added this between the studentData and main()

struct compare_student_by_id {
     bool operator() (const studentData & lhs, const studentData & rhs) { 
     return lhs.id < rhs.id; 
}};

and included

std::sort(myClass, myClass + 10, compare_student_by_id());

in the main.

Jake
  • 313
  • 1
  • 7
  • 16