Thread: sort ()

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    sort ()

    hi
    my sort algorithm is having problems

    its rather an unsual error
    can anyone understand to solve the error
    i am posting the entire code so that it can be tested

    the error takes place in the function named
    void Class::sort_roll ()

    ps. the problem is not as big as the lab its just one line
    cheers
    thanks

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cctype>
    #include <sstream>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <crtdbg.h>
    
    using namespace std;
    
    class StudentInfo;
    class Class;
    
    typedef vector <StudentInfo> StudentVect;
    
    const unsigned NumLabGrades = 7;
    const unsigned totalPossible = 300;
    ostream &operator<< (ostream &, StudentInfo &);
    Class & operator << (Class &, StudentInfo &);
    bool operator< (const StudentInfo&, const StudentInfo&);
    
    class Name
    {
    	char *first;
    	char *last;
    public:
    	Name () { first = last = NULL; }
    	void getFirstName (char *);
    	void getLastName (char *);
    	char * returnFirstName () const { return first; }
    	char * returnLastName () const { return last; }
    	~Name () { delete [] first; delete [] last; }
    };
    
    void Name::getFirstName (char * temp)
    {
    	first = new char [strlen(temp) + 1];
    	strcpy (first, temp);
    }
    
    void Name::getLastName (char * x)
    {
    	last = new char [strlen(x) + 1];
    	strcpy (last, x);
    }
    
    class StudentInfo
    {
    	Name full_name;
    	char ssn [10];
    	unsigned lab_grades [7], midterm, final;
    	unsigned loc;
    	unsigned total_points;
    	char final_grade;
    	float percent;
    public:
    	StudentInfo () {
    		loc = midterm = final = total_points = final_grade = 0; 
    		strcpy (ssn, "000000000");
    		for (unsigned bah = 0; bah < NumLabGrades; bah++) 
    			lab_grades [bah] = 0;
    		percent = 0;
    	}
    	~StudentInfo ();
    	Name & full_name_accessor ();
    	Name return_full_name () const;
    	void store_grades (unsigned, const int &);
    	void getSSN (char *);
    	void getMid (unsigned, const int &);
    	void getFinal (unsigned x, const int &);
    	bool checkLab (unsigned);
    	bool checkAlpha (unsigned);
    	void midterm_final_handler (unsigned &, char *);
    	string IntToString(unsigned num);
    	bool error_handler_101 (int, char *);
    	bool error_handler_101 (int, unsigned &, char *);
    	void calculateFinalGrade ();
    	unsigned return_midterm ();
    	char * return_ssn ();
    	unsigned * return_lab_grades ();
    	unsigned & return_loc () { return loc; }
    
    	StudentInfo & operator= (const StudentInfo &);
    
    	friend ostream & operator << (ostream &, StudentInfo &);
    	friend Class & operator << (Class &, StudentInfo &);
    	friend bool operator< (const StudentInfo& d1, const StudentInfo& d2);
    };
    
    Name & StudentInfo::full_name_accessor ()
    {
    	return full_name;
    }
    
    Name StudentInfo::return_full_name () const
    {
    	return full_name;
    }
    
    void StudentInfo::store_grades (unsigned x, const int & lin_num)
    {
    	if (!(checkLab (x))) {
    		if (!(error_handler_101 (lin_num, x, "Lab")))
    			return;
    	}
    	lab_grades [loc] = x;
    	loc++;
    }
    
    void StudentInfo::getMid (unsigned x, const int & line)
    {
    	if (!checkAlpha (x)) {
    		if (!(error_handler_101 (line, x, "Midterm")))
    			return;
    	}
    	if ( x<0 || x>50 ) {
    		cout << "Error with midterm score\n";
    		midterm_final_handler (x, "midterm");
    	}
    	midterm = x;
    }
    
    void StudentInfo::midterm_final_handler (unsigned & x, char * msg)
    {
    	cout << "What would you like to do: ";
    	cout << "\n[a]Abort Program\n[s]Skip (highly not recommended)\n[i]Input Score\n";
    	cout << "Enter Option: ";
    	char option;
    	cin >> option;
    	if (option == 's' || option == 'S') {
    		return;
    	}
    	else if (option == 'i' || option == 'I') {
    		unsigned temp;
    		cout << "Please input the " << msg << " score: ";
    		cin >> temp;
    		if (checkAlpha (temp)) {
    			x = temp;
    			return;
    		}
    	}
    	else {
    			cout << "Aborting Program.\nGoodbye\n";
    			exit (101);
    		}
    	return;
    }
    		
    void StudentInfo::getFinal (unsigned x, const int & line)
    {
    	if (!checkAlpha (x)) {
    		if (!(error_handler_101 (line, x, "Final")))
    			return;
    	}
    	if ( x<0 || x>100 ) {
    		cerr << "Error with final score\n";
    		midterm_final_handler (x, "final");
    	}
    	final = x;
    }
    
    bool StudentInfo::checkAlpha (unsigned x)
    {
    	string temp = IntToString (x);
    
    	for (unsigned i = 0; i < temp.length (); i++)
    	{
    		if(!isdigit (temp[i])) return 0;
    	}		
    	return 1;
    }
    
    string StudentInfo::IntToString(unsigned num)
    {
    	ostringstream myStream; //creates an ostringstream object
    	myStream << num << flush;
    	return(myStream.str()); //returns the string form of the stringstream object
    }
    
    StudentInfo::~StudentInfo ()
    {
    //	Name *temp = &full_name;
    //	delete [] temp;
    }
    
    void StudentInfo::getSSN (char * x)
    {
    	strcpy (ssn, x);
    }
    
    bool StudentInfo::checkLab (unsigned score)
    {
    	if (score < 0 || score > 25) 
    		return 0;
    	return 1;
    }
    
    void StudentInfo::calculateFinalGrade (void)
    {
    	int smallest = 0;
    	total_points = 0;
    
    	for (int j = 0; j < 5; j++)
    		if (lab_grades[j+1] < lab_grades [smallest])
    			smallest = j+1;
    
    	for (int i = 0; i < 6; i++)
    		if (i != smallest)
    			total_points += lab_grades[i];
    
    	total_points += lab_grades [NumLabGrades - 1] + midterm + final;
    	percent = ((float)total_points/(float)totalPossible) * 100;
    	
    	if (percent >= 90) final_grade = 'A';
    	else if (percent >= 80)	final_grade = 'B';
    	else if (percent >= 70)	final_grade = 'C';
    	else if (percent >= 60)	final_grade = 'D';
    	else final_grade = 'F';
    
    //cout << smallest << " " << total_points << " " << percent << " " << final_grade << endl;
    cout << "foo calculategrade" << endl;
    	return;
    }
    
    bool StudentInfo::error_handler_101 (int x, unsigned & score, char *check)
    {
    	if (check[0] == 'L')
    		cout << "There is an error with lab score # " << loc+1 << " on line number " << x << endl;
    	else
    		cout << "There is an error with the " << check <<" score: " << score << " on line number " << x << endl;
    
    	cout << "Choose Option:\n[a] Abort Program\n[s] Skip (not recommended)\n";
    	cout << "Enter Option: ";
    	char option = (char)cin.get ();
    
    	if (option == 's' || option == 'S') {
    		score = 0;
    		return 1;
    	}
    	else { // if not S then just abort even if incorrect option
    		cout << "Goodbye\n";
    		exit (101);
    	}
    }
    
    bool StudentInfo::error_handler_101 (int x, char * ssn)
    {
    	cout << "There is an error with the SSN# " << ssn << " on line number " << x << endl;
    	cout << "Choose Option:\n[a] Abort Program\n[s] Skip SSN (not recommended)\n";
    	cout << "Enter Option: ";
    	char option = (char) cin.get ();
    	if (option == 'a' || option == 'A') {
    		cout << "Goodbye\n";
    		exit (101);
    	}
    	else if (option == 's' || option == 'S') {
    		getSSN ("000000000");
    		return 1;
    	}
    	else ;
    	cout << flush;
    	return 0;
    }
    
    unsigned StudentInfo::return_midterm ()
    {
    	return midterm;
    }
    
    char * StudentInfo::return_ssn ()
    {
    	return ssn;
    }
    
    unsigned * StudentInfo::return_lab_grades ()
    {
    	return lab_grades;
    }
    
    StudentInfo & StudentInfo::operator= (const StudentInfo & vectAs)
    {
    	strcpy (ssn, vectAs.ssn);
    	for (unsigned i = 0; i < NumLabGrades; i++)
    	{
    		lab_grades[i] = vectAs.lab_grades[i];
    	}
    	midterm = vectAs.midterm;
    	final = vectAs.final;
    
    	full_name.getFirstName (vectAs.full_name.returnFirstName ());
    	full_name.getLastName (vectAs.full_name.returnLastName ());
    
    	return *this;
    
    }
    
    class Class
    {
    	StudentVect ptr;
    	unsigned size;
    public:
    	Class (unsigned x = 0);
    	~Class ();
    	void getInput ();
    	bool checkSSN (char x []);
    	void print ();
    	StudentVect return_ptr () { return ptr; }
    	void sort_roll ();
    	void printOut ();
    	bool UDsort (const StudentInfo &, const StudentInfo &);
    
    	static ifstream ifs;
    	static ofstream ofs;
    
    	friend ostream & operator << (ostream &, StudentInfo &);
    	friend bool operator< (const StudentInfo&, const StudentInfo&);
    	friend Class & operator << (Class &, StudentInfo &);
    };
    
    ifstream Class::ifs ("input.txt", ios::in | ios::binary);
    ofstream Class::ofs ("output.txt", ios::out);
    
    Class::Class (unsigned x) : size (x) 
    { 
    	StudentInfo defCon;
    	for (unsigned foo = 0; foo < x; foo++)
    		ptr.push_back (defCon);
    }
    
    void Class::getInput ()
    {
    	char temp[30], temp1[30], ssntemp [10], lab_grade [3], finalg [4];
    	int line_num = 1;
    	char check;
    	StudentInfo vectAs;
    
    	while (!Class::ifs.eof ()) {
    		Class::ifs.getline (temp, sizeof (temp), ',');
    		vectAs.full_name_accessor ().getFirstName (temp);
    
    		Class::ifs.getline (temp1, sizeof (temp1), ',');
    		vectAs.full_name_accessor ().getLastName (temp1);
    
    		Class::ifs.getline (ssntemp, sizeof (ssntemp), ',');
    		if (!checkSSN (ssntemp)) 
    			vectAs.error_handler_101 (line_num, ssntemp);
    		else 
    			vectAs.getSSN (ssntemp);
    
    		for (unsigned i = 0; i < NumLabGrades; i++)
    		{
    			if (Class::ifs.peek () == ',')
    			{
    				Class::ifs.ignore ();
    				vectAs.store_grades (0, line_num);
    			}
    			else
    			{
    				Class::ifs.getline (lab_grade, sizeof (lab_grade), ',');
    				vectAs.store_grades (atoi (lab_grade), line_num);
    			}
    		}
    		vectAs.return_loc () = 0;
    
    		Class::ifs.getline (lab_grade, sizeof (lab_grade), ',');
    		vectAs.getMid (atoi(lab_grade), line_num);
    
    		Class::ifs.get (finalg, sizeof (finalg), ',');
    		vectAs.getFinal (atoi (finalg), line_num);
    		
    		while ( check = (char) ifs.peek (), (check == (char)10 || check == (char)13))
    			ifs.ignore ();
    
    		ptr[line_num-1] = vectAs;		
    	
     		line_num++;
    	}
    	ifs.clear ();
    	ifs.close ();
    }
    
    bool Class::checkSSN (char x [])
    {
    	if (strlen (x) != 9) 
    		return 0;
    	for (unsigned i = 0; i < (unsigned) strlen (x); i++)
    		if (isdigit (x[i])) continue;
    		else return 0;
    	return 1;
    }
    
    void Class::print ()
    {
    	unsigned i;
    	cout << setw (55) << "CIS27 Class Grades Report\n\n";
    	cout << left << setw (22) << "Student Name";
    	cout << right << "--- SSN ---  ---- Lab Grades ----  Mid  Fin  Pts  Perct  G\n";
    	cout << setfill ('-') << setw (20) << "";
    	cout << "  ";
    	cout << setw (11) << "";
    	cout << "  ";
    	for (i = 0; i < 7; i++) cout << "-- ";
    	cout << " ";
    	for (i = 0; i < 3; i++) cout << "---  ";
    	cout << "-----  -\n";
    	cout << setfill (' ');
    
    	for (i = 0; i < size; i++)
    	{
    		cout << (ptr[i]);
    	}
    }
    
    void Class::printOut ()
    {
    	if (Class::ofs.fail ())
    	{
    		cerr << "Error creating output file\n.Did not create output file.\n";
    		return;
    	}
    	
    	Class foo;
    
    	unsigned i;
    	Class::ofs << setw (55) << "CIS27 Class Grades Report\n\n";
    	Class::ofs << left << setw (22) << "Student Name";
    	Class::ofs << right << "--- SSN ---  ---- Lab Grades ----  Mid  Fin  Pts  Perct  G\n";
    	Class::ofs << setfill ('-') << setw (20) << "";
    	Class::ofs << "  ";
    	Class::ofs << setw (11) << "";
    	Class::ofs << "  ";
    	for (i = 0; i < 7; i++) Class::ofs << "-- ";
    	Class::ofs << " ";
    	for (i = 0; i < 3; i++) Class::ofs << "---  ";
    	Class::ofs << "-----  -\n";
    	Class::ofs << setfill (' ');
    
    	for (i = 0; i < size; i++)
    		foo << (ptr[i]);
    
    	ofs.close ();
    }
    
    
    void Class::sort_roll ()
    {
    	sort (ptr.begin (),ptr.end (), UDsort);
    }
    
    bool Class::UDsort (const StudentInfo & x, const  StudentInfo & y)
    {
    	return x.return_full_name().returnLastName () < y.return_full_name().returnLastName () ||
               (!(y.return_full_name().returnLastName () < x.return_full_name().returnLastName ()) &&
                x.return_full_name().returnLastName () < y.return_full_name().returnLastName ());
    }
    
    Class & operator << (Class & temp, StudentInfo & x)
    {
    	unsigned i;
    	unsigned ln = strlen (x.full_name_accessor ().returnLastName ());
    	unsigned fn = strlen (x.full_name_accessor ().returnFirstName ());
    
    	Class::ofs << x.full_name_accessor ().returnLastName () << ", ";
    	if ((fn + ln) > 18)
    	{
    		for (i = 0; i < (18 - ln);i++)
    		{
    			Class::ofs << (x.full_name_accessor ().returnFirstName ())[i];
    		}
    	}
    	else 
    	{
    		Class::ofs << x.full_name_accessor ().returnFirstName ();
    		Class::ofs << setw (18-ln-fn) << "";
    	}
    	Class::ofs << setw (2) << "";
    	Class::ofs.write (x.ssn, 3);
    	Class::ofs << "-";
    	for (i = 3; i < 5; i++) Class::ofs << x.ssn[i];
    	Class::ofs << "-";
    	for (; i < 9; i++) Class::ofs << x.ssn[i];
    	Class::ofs << setw (2) << "";
    	for (i = 0; i < NumLabGrades; i++) {
    		Class::ofs << setw (2) << x.lab_grades [i] << " ";
    	}
    	Class::ofs << setw (2) << "" << x.midterm << "  " << setw (3) << right << x.final << "  ";
    	Class::ofs << setw (3) << x.total_points << "  " << setprecision (1) << fixed << x.percent << "%  " << x.final_grade << endl;
    	return temp;
    }
    
    ostream &operator<< (ostream &stream, StudentInfo & x)
    {
    	unsigned i;
    
    	/* another option: char temp [21]; strncat (temp, lastname, strlen (lastname));
    	strcat (temp, ", "); strncat (temp, firstname, (20 - strlen (lastname)); */
    
    	unsigned ln = strlen (x.full_name_accessor ().returnLastName ());
    	unsigned fn = strlen (x.full_name_accessor ().returnFirstName ());
    
    	stream << x.full_name_accessor ().returnLastName () << ", ";
    	if ((fn + ln) > 18)
    	{
    		for (i = 0; i < (18 - ln);i++)
    		{
    			stream << (x.full_name_accessor ().returnFirstName ())[i];
    		}
    	}
    	else 
    	{
    		stream << x.full_name_accessor ().returnFirstName ();
    		stream << setw (18-ln-fn) << "";
    	}
    	stream << setw (2) << "";
    	stream.write (x.ssn, 3);
    	stream << "-";
    	for (i = 3; i < 5; i++) stream << x.ssn[i];
    	stream << "-";
    	for (; i < 9; i++) stream << x.ssn[i];
    	stream << setw (2) << "";
    	for (i = 0; i < NumLabGrades; i++) {
    		stream << setw (2) << x.lab_grades [i] << " ";
    	}
    	stream << setw (2) << "" << x.midterm << "  " << setw (3) << right << x.final << "  ";
    	stream << setw (3) << x.total_points << "  " << setprecision (1) << fixed << x.percent << "%  " << x.final_grade << endl;
    	return stream;
    }
    
    Class::~Class ()
    {
    /*	for (unsigned i = 0; i < size; i++) delete ptr [i];
    	delete [] ptr;
    	*/
    	ptr.~vector ();
    }
    
    bool open_file_and_check_length (ifstream & ifs, unsigned & size)
    {
    	char temp [257];
    	ifs.open ("input.txt", ios::in | ios::binary);
    		if (!ifs) {
    			cout << "Cannot open file\n";
    			return false;
    		}
    		else {
    			while (ifs) {
    				ifs.getline (temp, sizeof (temp));
    				size++;
    			}
    		}
    	size--;
    	ifs.clear ();
    	ifs.close ();
    	return true;
    }
    
    bool operator< (const StudentInfo& x, const StudentInfo& y) 
    { 
    	return x.return_full_name().returnLastName () < y.return_full_name().returnLastName () ||
               (!(y.return_full_name().returnLastName () < x.return_full_name().returnLastName ()) &&
                x.return_full_name().returnLastName () < y.return_full_name().returnLastName ());
    }
    
    int main ()
    {
    	unsigned num_of_lines = 0;
    	ifstream ifs;
    	
    	if (!open_file_and_check_length (ifs, num_of_lines)) return 0;
    
    	Class cis27 (num_of_lines);
    	cis27.getInput ();
    	for (unsigned i = 0; i < num_of_lines; i++) ;
    //		cis27.return_ptr ()[i].calculateFinalGrade ();
    
    	cis27.sort_roll ();
    	cis27.print ();
    	cis27.printOut ();
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    71
    input file for testing

    www.mdofit.com/ide/27/lab9/input.txt

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>its rather an unsual error
    So unusual you can't describe it??

    I suggest you give a brief description as to how the problem manifests itself, and what you think it might be, rather than letting people second guess what they think it is.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    71
    yeah the problem is kinda like that

    it tells me

    d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\algorithm(1856): error C2064: term does not evaluate to a function


    and then when i click on it, it takes me into this deeper level code of the compiler which i cant understand. i cant even understand why is it coming up
    i tried it in VC 6

    and the error was

    d:\program files\microsoft visual studio\vc98\include\algorithm(633) : error C2064: term does not evaluate to a function
    d:\program files\microsoft visual studio\vc98\include\algorithm(598) : see reference to function template instantiation 'void __cdecl std::_Unguarded_insert(class StudentInfo *,class StudentInfo,int (__thiscall StudentInfo::*)(const class St
    udentInfo &,const class StudentInfo &))' being compiled

    i cant see what conversion problem is it having?

    to explain the program in a quick shot

    there r 3 classes, Name, StudentInfo, Class
    Name contains first and last name
    StudentInfo contains all the details of the students
    and Class is for the class section

    StudentInfo contains an object of Name
    i am suppose to create an array of StudentInfos in Class but i decided to create a vector (cuz i am an idiot )

    now putting the other problems aside, i am trying to get the sort algorithm to sort the array according to last and then first name.

    i am suspecting some conversion problem from the vector pointer (?) to the class arguments

    ???

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    is sort some kind of predefined function in one of hte libraries? seems like your passing the wrong type of arguments to it
    Last edited by Jamsan; 03-21-2003 at 07:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM