The class should also have a function named compareTo which compares the Employee object to an Employee object passed to it and returns 0 if they are equal, <0 if the current object is less than the passed object, and >0 if the current object is greater than the passed object. Use the strcmp function to compare the strings. The comparison should compare last names first, then first names (if the last names are equal), and finally ID# (if both names are equal).

class Record {
public:
   int id;
   char fname[20];
   char lname[20];
   int compareTo(const Record& r) const { //2nd const wont change member variables
      int ln = strcmp(lname, r.lname);
      int fn = strcmp(fname, r.fname);
      if (ln == 0)

         if (fn == 0)

      if (ln <0)
      if (ln >0)
      return 0;
   }
};

My question is am I using the strcmp correctly, for when it comes to comparing the lnames to each other? I know it is not complete, but I want to know if I am at least on the correct track or not.

All I want is a yes or no.

<code deleted>

Did I ask for you to do my homework? No. I asked if I was in the correct direction, a simple yes or no would suffice, not a snotty remark.

Did I ask for you to do my homework? No. I asked if I was in the correct direction, a simple yes or no would suffice,

No you didn't ask me to do your homework. And No I didn't realize that was all there was to your assignment. I apologize for helping you. I deleted all the code in my post so that you can have the joy of doing it yourself, unless of course you have already copied my code.

So, the answer to your questions I asked if I was in the correct direction is NO.

not a snotty remark.

What ??? I didn't say anything in my post -- just posted some code. I try not to make "snotty remarks" to anyone, so I'm sure you over-reacted or have me confused with someone else.

No you didn't ask me to do your homework. And No I didn't realize that was all there was to your assignment. I apologize for helping you. I deleted all the code in my post so that you can have the joy of doing it yourself, unless of course you have already copied my code.


What ??? I didn't say anything in my post -- just posted some code. I try not to make "snotty remarks" to anyone, so I'm sure you over-reacted or have me confused with someone else.

alright, not snotty but rude by the last comment of "And I WILL NOT DO YOUR HOMEWORK!"

There is much more to the assignment than what I posted. I just wanted to know if I was using the strcmp correctly. Your code that you had posted was like an earlier coding that I had used and could not get it to compile.

alright, not snotty but rude by the last comment of "And I WILL NOT DO YOUR HOMEWORK!"

Oh now I know what you mean. Look at all my posts -- that comment is in my signature and appears in every post I make. It is not directed to you, but to all newcomers. I have that in my signature because there are many many people who do actually ask us to do their homework for them. I'm happy to see that you are NOT among them :)

That is part of his forum signature ... it appears at the bottom of ALL posts he makes. It was not directed at you.

LOL we posted at the exact same time Ancient.

No you didn't ask me to do your homework. And No I didn't realize that was all there was to your assignment. I apologize for helping you. I deleted all the code in my post so that you can have the joy of doing it yourself, unless of course you have already copied my code.

So, the answer to your questions I asked if I was in the correct direction is NO.


What ??? I didn't say anything in my post -- just posted some code. I try not to make "snotty remarks" to anyone, so I'm sure you over-reacted or have me confused with someone else.

Thank you for your response. Now having you inform me I am not correct and my teacher saying that I am, I am more confused. I guess I will have to figure it out on my own. I am compiling my programs on Kermit through Linux. IDK if that changes anything. But thank you for your Help.

I apologize, I am new to this website and the last time I posted, I felt as though they were being rude to me. I'm sorry for thinking that you were.

You should listen to your teacher first. A simple yes or no does nothing to help you understand the problem. From what you posted you should first use strcmp() to see if the last names are the same. If they are the same, then use strcmp() to see if the first names are the same. If they are also the same then check the id.

One way to do it is somthing like you posted, that is to call strcmp() for all three cases saving the results in different int variables, then check those variables for equality. That, however, is the long way around the problem because if, for example, the last names are different then it was not necessary to compare the first names or id's. A more efficient solution is to only call strcmp() when absolutely necessary to do so.

Thank you for being more helpful lol I tend to do everything the hard way. I'm known for writing the same program over and over again and usually finding that I am missing a bracket or a semicolon.

I've seen from other website postings that individuals are using if (!strcmp(lname, r.lname)) or if (strcmp(lname, r.lname) =0) to compare the two strings. Does it truely matter which one I use?

It doesn't matter which one you use as long as you understand the not operator. I prefer the "== 0" form myself because it doesn't require so much thinking to know what is going on. But, unless told otherwise by your teacher, either one should be acceptable.

The class should also have a function named compareTo which compares the Employee object to an Employee object passed to it and returns 0 if they are equal, <0 if the current object is less than the passed object, and >0 if the current object is greater than the passed object. Use the strcmp function to compare the strings. The comparison should compare last names first, then first names (if the last names are equal), and finally ID# (if both names are equal).

Read all the records from the empdata.txt file into the array. Display how many records were read in, then sort the records using your own modified version of the Quicksort algorithm or the Heapsort algorithm.

questions: I'm still having issues with the strmcpy. Typically I have problems with someone and then I figure it out later on. Right now, I'm wondering if I should use what I have for strmcpy or if I should use the variable names instead. (int ln = strcmp(lname, r.lname); int fn = strcmp(fname, r.fname); int idd = strcmp(id, r.id); )

another question: While reading the information in to the arrays from the file, in the while loop, should I include the compareTo function and the quickSort function or one or the other or both? A simple "yes compareTo in loop" or "no compareTo in loop" would suffice. I figured that both would work in the loop due to wanting to compare and quicksort while reading from the file.

#include <iostream>
#include <fstream>
#include <cstring>
using std::cout;
using std::ifstream;
using std::endl;
using std::strcpy;
using std::strcmp;

class Record {
public:
   int id;
   char fname[20];
   char lname[20];
   int compareTo(const Record& r) const { //2nd const wont change member variables
      if (strcmp(lname, r.lname) == 0)
         return r.lname;
         if(strcmp(fname, r.fname) == 0)
            return r.fname;
            if(strcmp(id, r.id) == 0)
               return r.id;
      if (strcmp(lname, r.lname)  <0 )
         return 0;
      if (strcmp(lname, r.lname)  >0)
         return 0;
   }
};

void quickSort(Record a[], int left, int right);

int main(void) {
   Record recs[120000];
   long numrecs = 0;
   char linein[60];
   char* ptr;

   ifstream ifile("/examples/empdata.txt");
   if (!ifile) {
      cout << "Could not open input file\n\n";
      return 1;
   }

   ifile >> linein;
   while (ifile) {
      //grab first field
      ptr = strtok(linein, ":");
      if (ptr==NULL) { return 2; }
         recs[numrecs].id = atoi(ptr);

      //grab second field
      ptr = strtok(NULL, ":");
      if (ptr==NULL) { return 2; }
         strcpy(recs[numrecs].fname, ptr);

      //grab third field
      ptr = strtok(NULL, ":");
      if (ptr==NULL) { return 2; }
         strcpy(recs[numrecs].lname, ptr);

      //see if any more fields exist
      ptr = strtok(NULL, ":");
      if (ptr!=NULL) { return 3; }

   numrecs++;
   ifile >> linein;
//   compareTo();
//   quickSort(work, 0, ARRAY_SIZE-1);
   }

   cout << "Total records: " << endl; //recs[numrecs] << endl;
   cout << "  ID#    Name" << endl;
   cout <<"------  ---------------" << endl;
   for (int i=0; i<15; i++) {
      cout << recs[i].id << ": " << recs[i].lname << ", " << recs[i].fname << endl;
   }

   ifile.close();

   return 0;
}
void quickSort(Record a[], int left, int right) {
   int pivotNdx, l_hold = left, r_hold = right;
   Record pivot = a;
   while (left < right) {
      while ((a.compareTo(pivot)>=0) && (left < right))
right--;
      if (left != right) a[left++] = a;
      while ((a.compareTo(pivot)<=0) && (left <
right)) left++;
      if (left != right) a[right--] = a;
   }
   a = pivot;
   pivotNdx = left;
   left = l_hold;
   right = r_hold;
   if (left < pivotNdx) quickSort(a, left, pivotNdx-1);
   if (right > pivotNdx) quickSort(a, pivotNdx+1, right);
}

questions: I'm still having issues with the strmcpy. Typically I have problems with someone and then I figure it out later on. Right now, I'm wondering if I should use what I have for strmcpy or if I should use the variable names instead. (int ln = strcmp(lname, r.lname); int fn = strcmp(fname, r.fname); int idd = strcmp(id, r.id); )

There is no strmcpy. There is strcmp, strcpy, and strncpy. It looks like you are using strcmp and strcpy. In this function, which returns an integer,

int compareTo(const Record& r) const { //2nd const wont change member variables
      if (strcmp(lname, r.lname) == 0)
         return r.lname;
         if(strcmp(fname, r.fname) == 0)
            return r.fname;
            if(strcmp(id, r.id) == 0)
               return r.id;
      if (strcmp(lname, r.lname)  <0 )
         return 0;
      if (strcmp(lname, r.lname)  >0)
         return 0;
   }

you are returning this:

return r.lname;

which is not an integer:

char lname[20];

Instead, you want to go on to the first name if the comparison of last names EQUALS 0. You want to return something if the comparison of last names is NOT EQUAL. Currently you have it the other way. What to return? The function wants an integer returned and that integer should be negative if the "current object is less than the passed object", which is exactly what the strcmp function will give you in the way you have it set up. So change from this:

if (strcmp(lname, r.lname) == 0)
         return r.lname;

to this:

if (strcmp(lname, r.lname) != 0) // return when not equal
         return strcmp(lname, r.lname);

Change similarly for the rest of the function.

Here is one solution for your compareTo() function

int compareTo(const Record& r) const  //2nd const wont change member variables
{
	// first compare last names 
	int result = strcmp(lname, r.lname);

	if(0 != result)
	{
		// last names differ
		return result;
	}

	// then compare first names 
	result = strcmp(fname, r.fname); 
 
	if(0 != result)
	{
		// first names differ
		return result;
	}

	// then ids
	result = strcmp(id, r.id);

	// nothing more to compare, 
	// just return result whatever it 
	// is now (-1,0 or 1)
	return result;
}

There is no strmcpy. There is strcmp, strcpy, and strncpy. It looks like you are using strcmp and strcpy. In this function, which returns an integer,

int compareTo(const Record& r) const { //2nd const wont change member variables
      if (strcmp(lname, r.lname) == 0)
         return r.lname;
         if(strcmp(fname, r.fname) == 0)
            return r.fname;
            if(strcmp(id, r.id) == 0)
               return r.id;
      if (strcmp(lname, r.lname)  <0 )
         return 0;
      if (strcmp(lname, r.lname)  >0)
         return 0;
   }

you are returning this:

return r.lname;

which is not an integer:

char lname[20];

Instead, you want to go on to the first name if the comparison of last names EQUALS 0. You want to return something if the comparison of last names is NOT EQUAL. Currently you have it the other way. What to return? The function wants an integer returned and that integer should be negative if the "current object is less than the passed object", which is exactly what the strcmp function will give you in the way you have it set up. So change from this:

if (strcmp(lname, r.lname) == 0)
         return r.lname;

to this:

if (strcmp(lname, r.lname) != 0) // return when not equal
         return strcmp(lname, r.lname);

Change similarly for the rest of the function.

*rolls eyes* my bad, just needed != instead of ==, see I told you that there are sometimes just a few simple things I need to change.

Hmm, that is a bit inefficient because it calls the strcmp two times with the same parameters,
so I'd suggest you really to use something like:

int result =strcmp(lname, r.lname);
if(0 != result)
{
     // last names differ, return result
     return result;
}
// compare the first names in same fashion
...
// compare the ids
...
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.