Im pretty new to coding in C++ and Im a student at devry unversity. I have to sort this class by credit hours in ascending order and Im having some trouble. If anyone could help it would be greatly appreciated.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Student 
{
private:
    int id;
    string name;
    string lname;
    char gender;
    string dob;
    string major;
    int credits;
    double gpa;



public:
    int getcredits() const {
        return credits;
    }
    Student();
    void setStudent(int, string, string, char, string, string, int, double); 
    void getStudent(); 
};
Student::Student()
{   id=6984; name="John"; lname="Trovota"; gender='M'; dob="1/1/1980"; major="CIS"; credits=64; gpa=3.75; }
void Student::setStudent(int i, std::string n, std::string l, char g, std::string d, std::string m, int c, double p) 
{      id=i; name=n; lname=l; gender=g; dob=d; major=m; credits=c; gpa=p;    }
void Student::getStudent() 
{ cout<<id<<"  "<<name<<" "<<lname<<"  "<<gender<<"  "<<dob<<"  "<<major<<"  "<<credits<<"  "<<gpa<<endl; }

bool cmp( const Student * a, const Student * b ) {
    return a->getcredits() < b->getcredits() ;
}



int main()
{
    vector <Student*> v;
    sort( v.begin(), v.end(), cmp );

    Student s1;
    s1.getStudent();
    Student s2;
    s2.setStudent(2323, "Brittney", "Speer", 'F', "5/23/1984", "BIS", 78, 2.98 );
    s2.getStudent();
    Student s3;
    s3.setStudent(1452, "Kathy", "Johnson", 'F', "12/25/1982", "CIS", 30, 3.33 );
    s3.getStudent();
    Student s4;
    s4.setStudent(4321, "Bill", "Newton", 'M', "7/8/1976", "BIS", 100, 3.95 );
    s4.getStudent();
    system ("pause");
    return 0;
}

You have not populated the vector v with student class objects. After you do that then you can call std::sort() to sort the vector. pass std::sort() the cmp() function you have already written as the last parameter.

Ancient I am not quite understanding what you mean. Could you include an example?

Line 47 is too early because the vector doesn't contain anything at that point. Programs run from top to bottom, so the first thing in that main() that gets executed is line 47. You need to move that line all the way down to between lines 59 and 60.

Next, you have to add the 4 objects to the vector. Just declaring them doesn't do that. For example, between lines 53 and 54 you need to add v.push_back(&s2); Do something similar after each of the other student objects have been initialized.

so I've done some more work on this code and here's what it looks like now.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Student 
{
private:
    int id;
    string name;
    string lname;
    char gender;
    string dob;
    string major;
    double gpa;



public:

    int credits;
    Student();
    void setStudent(int, string, string, char, string, string, int, double); 
    void getStudent();
    bool sortVecByCreditsAscending(Student &s1, Student &s2);

};
Student::Student()
{   id=6984; name="John"; lname="Trovota"; gender='M'; dob="1/1/1980"; major="CIS"; credits=64; gpa=3.75; }
void Student::setStudent(int i, std::string n, std::string l, char g, std::string d, std::string m, int c, double p) 
{      id=i; name=n; lname=l; gender=g; dob=d; major=m; credits=c; gpa=p;    }
void Student::getStudent() 
{ cout<<id<<"  "<<name<<" "<<lname<<"  "<<gender<<"  "<<dob<<"  "<<major<<"  "<<credits<<"  "<<gpa<<endl; }


int main()
{
    std::vector<Student> userData;

    Student s1;
    userData.push_back(s1);
    s1.getStudent();
    Student s2;
    s2.setStudent(2323, "Brittney", "Speer", 'F', "5/23/1984", "BIS", 78, 2.98 );
    userData.push_back(s2);
    s2.getStudent();
    Student s3;
    s3.setStudent(1452, "Kathy", "Johnson", 'F', "12/25/1982", "CIS", 30, 3.33 );
    userData.push_back(s3);
    s3.getStudent();
    Student s4;
    s4.setStudent(4321, "Bill", "Newton", 'M', "7/8/1976", "BIS", 100, 3.95 );
    userData.push_back(s4);
    s4.getStudent();

    std::sort(userData.begin(), userData.end(), sortVecByCreditsAscending);

    bool sortVecByCreditsAscending(Student &s1, Student &s2);
    return s1.credits < s2.credits;


    system ("pause");
    return 0;
}

This is the closest I have come to getting it to work. I am throwing only one error code on line 59. The error is as follows.

2010\projects\cis247h2-2\cis247h2-2\cis247h2-2.cpp(59): error C2065: 'sortVecByCreditsAscending' : undeclared identifier

Thats because line 59 is the first time in the file that the identifier sortVecByCreditsAscending is mentioned so as far as the compiler is concerned it is undeclared, hence the error.

Line 61 and 62 are wrong, 61 declares a function and 62 returns from main, not what you intended. The cmp function from your first code listing does what you want, I would reinstate that and then change line 59 to

std::sort(userData.begin(), userData.end(), cmp);

I'm not sure if I am misunderstanding or coding this wrong. when I replaced line 59 with std::sort(userData.begin(), userData.end(), cmp); I recieved the same error just with cmp as the undeclared identifier. Could you elaberate a little more?

put the cmp() function back the way you had it in your first post, there was no reason to change it. Then just delete line 61 because you don't need a function named sortVecByCreditsAscending.

so this is now what I have.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Student 
{
private:
    int id;
    string name;
    string lname;
    char gender;
    string dob;
    string major;
    double gpa;



public:

    int credits;
    Student();
    void setStudent(int, string, string, char, string, string, int, double); 
    void getStudent();

};
Student::Student()
{   id=6984; name="John"; lname="Trovota"; gender='M'; dob="1/1/1980"; major="CIS"; credits=64; gpa=3.75; }
void Student::setStudent(int i, std::string n, std::string l, char g, std::string d, std::string m, int c, double p) 
{      id=i; name=n; lname=l; gender=g; dob=d; major=m; credits=c; gpa=p;    }
void Student::getStudent() 
{ cout<<id<<"  "<<name<<" "<<lname<<"  "<<gender<<"  "<<dob<<"  "<<major<<"  "<<credits<<"  "<<gpa<<endl; }

bool cmp( Student * a, Student * b ) {
return a->credits() < b->credits() ;
}


int main()
{
    vector <Student> userData;

    Student s1;
    userData.push_back(s1);
    s1.getStudent();
    Student s2;
    s2.setStudent(2323, "Brittney", "Speer", 'F', "5/23/1984", "BIS", 78, 2.98 );
    userData.push_back(s2);
    s2.getStudent();
    Student s3;
    s3.setStudent(1452, "Kathy", "Johnson", 'F', "12/25/1982", "CIS", 30, 3.33 );
    userData.push_back(s3);
    s3.getStudent();
    Student s4;
    s4.setStudent(4321, "Bill", "Newton", 'M', "7/8/1976", "BIS", 100, 3.95 );
    userData.push_back(s4);
    s4.getStudent();

    std::sort(userData.begin(), userData.end(), cmp);

    return s1.credits < s2.credits;


    system ("pause");
    return 0;
}

The code is still not compiling. I am getting the following error.
2010\projects\cis247h2-2\cis247h2-2\cis247h2-2.cpp(38): error C2064: term does not evaluate to a function taking 0 arguments

If you could include code so I have a visual of what this is suppose to look like, that would be appreciated. I am very new to this and unfortunately my teacher is useless.

Student::credits is NOT a function, which is what the error message told you. Line 38 is wrong -- remove the parentheses.

So I got the code to compile finally, but the output is still not sorted. I am not understanding what is going on?

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Student 
{
private:
    int id;
    string name;
    string lname;
    char gender;
    string dob;
    string major;
    double gpa;



public:

    int credits;
    Student();
    void setStudent(int, string, string, char, string, string, int, double); 
    void getStudent();

};
Student::Student()
{   id=6984; name="John"; lname="Trovota"; gender='M'; dob="1/1/1980"; major="CIS"; credits=64; gpa=3.75; }
void Student::setStudent(int i, std::string n, std::string l, char g, std::string d, std::string m, int c, double p) 
{      id=i; name=n; lname=l; gender=g; dob=d; major=m; credits=c; gpa=p;    }
void Student::getStudent() 
{ cout<<id<<"  "<<name<<" "<<lname<<"  "<<gender<<"  "<<dob<<"  "<<major<<"  "<<credits<<"  "<<gpa<<endl; }

bool cmp( Student s1, Student s2 ) {
return s1.credits < s2.credits ;
}


int main()
{
    vector <Student> userData;

    Student s1;
    userData.push_back(s1);
    s1.getStudent();
    Student s2;
    s2.setStudent(2323, "Brittney", "Speer", 'F', "5/23/1984", "BIS", 78, 2.98 );
    userData.push_back(s2);
    s2.getStudent();
    Student s3;
    s3.setStudent(1452, "Kathy", "Johnson", 'F', "12/25/1982", "CIS", 30, 3.33 );
    userData.push_back(s3);
    s3.getStudent();
    Student s4;
    s4.setStudent(4321, "Bill", "Newton", 'M', "7/8/1976", "BIS", 100, 3.95 );
    userData.push_back(s4);
    s4.getStudent();

    std::sort(userData.begin(), userData.end(), cmp);
    return s1.credits < s2.credits;



    system ("pause");
    return 0;
}

This is the
output:

6984 John Trovota M 1/1/1980 CIS 64 3.75
2323 Brittney Speer F 5/23/1984 BIS 78 2.98
1452 Kathy Johnson F 12/25/1982 CIS 30 3.33
4321 Bill Newton M 7/8/1976 BIS 100 3.95

It should be sorting the credit hours by ascending order, Kathy first, John second, Brittney third, and Bill fourth.

  1. Delete line 63 --

  2. You have to call getStudent() after the vector is sorted, not before. Call getStudent() from the vector, not the individul Student objects. You need to delete all those lines that call getStudent() and replace them with the following loop AFTER the array has been sorted.

    for(int i = 0; i < userData.size(); i++)
    {
    userData[i].getStudent();
    }

I don't understand what you mean by calling getStudent from the vector?

look at the code (loop) I posted

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Student 
{
private:
    int id;
    string name;
    string lname;
    char gender;
    string dob;
    string major;
    double gpa;



public:

    int credits;
    Student();
    void setStudent(int, string, string, char, string, string, int, double); 
    void getStudent();

};
Student::Student()
{   id=6984; name="John"; lname="Trovota"; gender='M'; dob="1/1/1980"; major="CIS"; credits=64; gpa=3.75; }
void Student::setStudent(int i, std::string n, std::string l, char g, std::string d, std::string m, int c, double p) 
{      id=i; name=n; lname=l; gender=g; dob=d; major=m; credits=c; gpa=p;    }
void Student::getStudent() 
{ cout<<id<<"  "<<name<<" "<<lname<<"  "<<gender<<"  "<<dob<<"  "<<major<<"  "<<credits<<"  "<<gpa<<endl; }

bool cmp( Student s1, Student s2 ) {
return s1.credits < s2.credits ;
}


int main()
{
    vector <Student> userData;

    Student s1;
    userData.push_back(s1);
//    s1.getStudent();
    Student s2;
    s2.setStudent(2323, "Brittney", "Speer", 'F', "5/23/1984", "BIS", 78, 2.98 );
    userData.push_back(s2);
//    s2.getStudent();
    Student s3;
    s3.setStudent(1452, "Kathy", "Johnson", 'F', "12/25/1982", "CIS", 30, 3.33 );
    userData.push_back(s3);
//    s3.getStudent();
    Student s4;
    s4.setStudent(4321, "Bill", "Newton", 'M', "7/8/1976", "BIS", 100, 3.95 );
    userData.push_back(s4);
//    s4.getStudent();

    std::sort(userData.begin(), userData.end(), cmp);
    for(size_t i = 0; i < userData.size(); ++i)
        userData[i].getStudent();


    system ("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.