4 Replies - 920 Views - Last Post: 20 April 2009 - 09:41 AM Rate Topic: -----

#1 kodefrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 09

Student sorting program

Posted 15 April 2009 - 04:30 PM

Hello everybody! I just became a member, and this is my very first post here. I am taking C++ as an extra subject the first year I am studying computer engineering. I am now preparing for the exam in C++, and doing old C++ exams in preparation for the upcoming exam.

I recently completed a program that was part of a previous exam (it counted 50% of the exam), and I paste that code below. This code is not any assignment I have to deliver to anyone, it is purely something that I do to become a better coder. What I am wondering is wheter I could do this better? Is there any glaring mistakes in my program, or anything that could have been done in an easier way? I think the code and its comments should be sufficient to get a grasp about what the program is all about.. If there is any part of the program that you need me to elaborate on, please ask questions, and I will do my best to answer them. I hope this is the right forum for this post, I saw there is a homework forum, but as this is not homework, but rather just something I do to learn more C++, I tough that this forum would be appropriate.

Thank you very much in advance for helping me! Oh.. The indenting might be a bit off. I write the code using putty connecting to unbuntu running in wmware player on my laptop, and I use vim as my editor, and to get the code into this post, I used WinSCP to connect to the virtual linuxbox, and just opened the file and pasted it here. The source code looked very good in vim, but the indentation might be a little bit weird when I paste it. I don't know why, perhaps it is because the file is opened in a windows editor and created in a vim-editor? Anyway, I think the code should be readable anyway.

Thanks again!



// Task of program:
// 
// Read student numbers and names from a tekst-file, sort them based
// on the studentnumber and write them to a new file.
// The function leggInnSortert (putInSorted) has to be a template function

//Sample input-file studenter.dat:

/*
--File start------------------------------
123456 Per Tangen 
333333 Ole Hansen
444444 Morten Hanstvedt
114444 Marie Flaktvedt
222222 Olga Martinsen

--File end------------------------------
*/

// The output-file studenter2.dat after the program has 
// worked with this input file:

// Sortet based on Student numbers

/*
--File start-------------------------------
114444 Marie Flaktvedt
123456 Per Tangen 
222222 Olga Martinsen
333333 Ole Hansen
444444 Morten Hanstvedt 
--File end--------------------------------


*/

// Output from program when run and studender.dat exists 
// as prescribed above:

/*
--Console output start----------------------------------
123456 Per Rommetveit :: Inserted: 123456  Per Rommetveit to end ...1 items in array
333333 Ole Hansen :: Inserted: 333333  Ole Hansen to end ...2 items in array
444444 Morten Hanstvedt :: Inserted: 444444  Morten Hanstvedt to end ...3 items in array
114444 Marie Flaktvedt :: Inserted: 114444  Marie Flaktvedt to head ...4 items in array
222222 Olga Martinsen :: Inserted: 222222  Olga Martinsen to middle ...5 items in array
Finished sorting!
138812 Kari Olsen kommer etter 123456 Ole Hansen
--Console output end----------------------------------
*/



#include <iostream>
#include <string.h>
#include <string>
#include <fstream>
#include <cstdlib> // for exit function

#define BIGNUM 100

using namespace std;

class Student {
	public:
		Student(){};	
		Student(int studnr, char n[]);
		bool operator==(const Student &andre) const;  
		bool operator<(const Student &rhs) const; 
		bool operator>=(const Student &rhs) const; 
		friend ostream& operator<< (ostream& ostr, const Student& andre);
		friend void skrivSortert(const char* innfil, const char* utfil);
	private:
		int studentnummer;
		char navn[21];
};


Student::Student(int studnr, char n[]) {
	studentnummer = studnr;
	int i = 0;
	strcpy(navn,n);
}


bool Student::operator==(const Student &andre) const {
	if(studentnummer == andre.studentnummer)
		return 1;
	return 0;
}


bool Student::operator<(const Student &rhs) const {
	if(studentnummer < rhs.studentnummer)
		return 1;
	return 0;
}


bool Student::operator>=(const Student &rhs) const {
	if (studentnummer >= rhs.studentnummer)
		return 1;
	return 0;
}


ostream& operator<< (ostream& ostr, const Student& andre) {
	ostr << andre.studentnummer << " " << andre.navn;
	return ostr;
} 


template <typename T>
void leggInnSortert(T a[], T t, int &antall) {

	int i;

	// We must determine if number is to be inserted first, in the "middle" or at the end

	if(t < a[0]) { // insert to head
		if(antall != 0){ //shift all values one up in the index
			for(int u=antall;u>=0;u--)
				a[u+1]=a[u];
		}

		a[0]=t;
		antall++;

	cout << "Inserted: " << t << " to head ";

	} else if ( t >= a[antall-1] ) { //insert to end
		cout << "Inserted: " << t << " to end ";
		a[antall] = t;
		antall++;

	} else { // insert in middle
		cout << "Inserted: " << t << " to middle ";

		i=0;
		while(t >= a[i]) {
			i++;
		}

		for(int u=antall;u>=i;u--)
			a[u+1]=a[u];

		a[i]=t;
		antall++;
	}

	cout << "..." << antall << " items in array" << endl;

}

void skrivSortert(const char* innfil, const char* utfil) {


	ifstream in;						  // input stream
	ofstream out;						// output stream


	// Variables to hold information that is read from the file
	int nr;
	char first[21];
	char last[21];
	char fullname[21] = "";

	// Store a blank 
	char space[] = {" "};

	// Creating a list of students
	Student list[BIGNUM];

	
	int count = 0;
	int &c = count; 					// Making c and count twins

	
	out.open (utfil,ofstream::out);		// Opening outfile
	in.open (innfil,ifstream::in);		// Opening infile


	if(in.fail() || out.fail()) {
		cout << "Problem opening files\n";
		exit(1);
	  }


	while (!in.eof()) {

		in >> nr >> first >> last;	// Read from file into variables
		if(nr == 0) continue;		  // Ugly hack, if this is removed, the loop will 
									// read an empty item!

		//Preparing fullname variable
		strcat(fullname,space);
		strcat(fullname,first);
		strcat(fullname,space);
		strcat(fullname,last);

		cout << nr << fullname << " :: ";

		Student x(nr, fullname);   // Make Student object 

		leggInnSortert(list,x,c);  // Insert Student object to list and sort it on student number 


		//Clear all the variables
		memset(first,0,sizeof(first));
		memset(last,0,sizeof(last));
		memset(fullname,0,sizeof(fullname));
		nr=0;
}


	// Write data to out file
	for(int i=0;(i+1)<=count;i++)
		out << list[i].studentnummer << list[i].navn << endl; 

	// Close the files
	in.close();
	out.close();

cout << "Finished sorting!";
}



int main(void) {


	skrivSortert("studenter.dat","studenter2.dat");

	cout << "\n";

	// Don't care about the stuff below, it is just to test the Student functions

	Student a(138812, "Kari Olsen");
	Student b(123456, "Ole Hansen");
	cout << a;
	if(a<b) cout << " kommer foran ";
	else if(a==b) cout << " er lik ";
	else cout << " kommer etter ";
	cout << b << endl;

	return 0;
}






This post has been edited by kodefrog: 15 April 2009 - 04:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Student sorting program

#2 kodefrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 09

Re: Student sorting program

Posted 16 April 2009 - 03:13 PM

I am wondering why there is no response to my above post? Nobody that could tell me if there is something in the code that could have been done in a better manner? I am a beginner, and I guess the code is not perfect, so please tell me if there is any way it could be more efficient.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Student sorting program

Posted 16 April 2009 - 04:11 PM

One would say that it should be the job of your professor/instructor to critique your code; we're really much more about answering specific questions.

The main thing that sticks out to me is that you've left yourself open to buffer overflows with your character arrays. For example,
char first[21];
char last[21];
char fullname[21] = "";

// Store a blank
char space[] = {" "};
...
//Preparing fullname variable
strcat(fullname,space);
strcat(fullname,first);
strcat(fullname,space);
strcat(fullname,last);



Your fullname array only has enough space for 20 characters, yet your first and last can also contain that many on their own, plus you're adding two spaces. This problem continues into your Student class itself, where the name can again only hold 20 characters.

If you're using C++, I would just go with the C++ string class.

This:
for(int i=0;(i+1)<=count;i++)
is also very non-standard. It should simply be
for(int i=0; i < count; i++)

Was This Post Helpful? 0
  • +
  • -

#4 kodefrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 09

Re: Student sorting program

Posted 20 April 2009 - 09:38 AM

Thank you very much for taking your time to answer Jack! I will have a spesific question the next time I ask about something C++ related!
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Re: Student sorting program

Posted 20 April 2009 - 09:41 AM

Moved to C++ Homework.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1