Basic class programming assignment questions and improvements/review

something
Last edited on
1. No, that's fine.
2&4. If you can't use string.h, then a for loop would be the next best thing.
Some small things you should do:
- It would be a good idea to set both parameters to be const char*. If you haven't talked about const in class yet, then I suppose you don't need to, just know that const-correctness is a good thing (especially for classes). http://www.parashift.com/c++-faq/const-correctness.html
- Make the size of the character arrays 32. The first and last names can be up to 31 characters. If they are exactly 31 characters, then there's no room for the null byte, which is necessary for c-strings.
- Check the lengths of the first and last name in .set_name(). Alternatively (since you can't use strlen()), you can copy from fname and lname until you hit the null byte for each. You can do this using a for loop:
1
2
char src[32], dest[32];
for(int i = 0; i < 32 && (dest[i] = src[i]) != '\0'; ++i); // a while loop can also work if this looks too ugly 

3. The way that you said how it works in the comment doesn't match up with how you did it. The comment says to do this:
*sal = m_dSalary; copying the value of the salary to wherever sal points. It doesn't really do anything as it is now.
Last edited on
1. Great, thank you.
2. Yes I used a for loop, did not check for null bit because I wasn't sure if the instructor would change the length of the passed array. I'm sure he will, and I will do this.
-You are correct about changing the char array sizes to [32] also, I was counting minus the null bit for some reason. Thank you.
-I will definitely look at const. I can't change these function parameters, but it will be great to understand why in the future I need to do this.

3. The way I have it works somehow. The comment is almost directly from the SOW file he gave me. I originally had *sal = m_dSalary and received errors, so I changed it to what I thought might work, not really knowing why. My attempt to explain to myself what was happening seemed off, thus the question.

Edit: Just changed that line of code in 3. to *sal = m_dSalary again. It works now. I still don't really understand this part.

3a. Why does my original code work if you say it does nothing? I'm not trying to argue, I just don't completely understand. I.E. employeeOne.getSalary(&salary);
Paired with
1
2
3
4
void EmployeeRecord::getSalary(double *sal)
{
    sal = &m_dSalary;
}


3b. Also, this code employeeOne.getSalary(&salary); Paired with
1
2
3
4
   void EmployeeRecord::getSalary(double *sal)
{
	*sal = m_dSalary;
}

Previously did not work, I'm sure I had something missing...but I still don't understand the correct way to think of this.

Regardless, thank you so much.
Last edited on
1
2
3
4
void EmployeeRecord::getSalary(double *sal)
{
    sal = &m_dSalary;
}

The above function receives a pointer that is pointing to an address in memory (in this case, address in main()). sal = &m_dSalary; sets the pointer sal (which is a local variable to this function) to point to the address of m_dSalary. You have now lost the address of the original variable that you wanted to change. In the end, this function actually doesn't do anything of importance. The value of address in main() has not changed and sal is destroyed at the end of the function.

*sal = m_dSalary; will first dereference sal and copy the value of m_dSalary to wherever sal is pointing (it will write to address in main()). I'm not sure why it didn't work previously. Do you remember what the errors were?

This site has a page about pointers: http://www.cplusplus.com/doc/tutorial/pointers/

And youtube has a cute visual http://www.youtube.com/watch?v=mnXkiAKbUPg
Last edited on
Actually, I believe the error was there originally because I wasn't passing the address of salary from main and nothing to do with the actual getSalary() function. I kept reading last night until I understood this and why it works, so that solves that. Thank you.

The call of sal = &m_dSalary worked correctly. If I set m_dSalary to any number via class constructor, the number would be passed into double salary = 0.0; and would print correctly. My only reasoning for this working was that since *sal points to an address in main, setting sal = &m_dSalary; means that the pointer(now pointing to the new address of m_dSalary passes this address into the address of the double salary variable in main.

I'm not saying this is what is happening, but that's what I told myself because every value I tested worked correctly.

Either way man, thanks for all the help and marking as solved.
Last edited on
Topic archived. No new replies allowed.