I've created my code in Microsoft Visual Studio Express 2013 and it compiles and runs fine. I've moved this over to Linux and had compile errors. To fix it, I had to add in #include <cstring> on my student.h file and it compiles fine. However now it does not give me the correct output. Is there a difference between the strcmp and strncpy in Linux? Or am I missing a step to make it work on Linux? The GPA's are coming as 0 and 6.95281e-310 instead of the 3.9 and 3.5.
#include "student.h"
//implement the required 3 functions here
Student::Student(constchar initId[], double gpa) : gpa(gpa)
{
// initialize a newly created student object with the passed in value
strncpy_s(id, initId, Student::MAX_CHAR - 1);
id[Student::MAX_CHAR - 1] = '\0';
}
bool Student::isLessThanByID(const Student& aStudent) const
{
// compare the current student object with the passed in one by id.
if (strcmp(id, aStudent.id) > 0)
{
returntrue;
}
else
{
returnfalse;
}
}
bool Student::isLessThanByGpa(const Student& aStudent) const
{
// compare the current student object with the passed in one by gpa
if (gpa < aStudent.gpa)
{
returntrue;
}
else
{
returnfalse;
}
}
void Student::print() const
{
cout << id << '\t' << gpa << endl;
}
I also revised that section slightly. It still gives me the correct input in windows but not in Linux. As as far as everything I was reading strncpy only needs three. Is there something different I should be using?
1 2 3 4 5 6 7 8 9 10 11 12
Student::Student(constchar initId[], double gpa) : gpa(gpa)
{
// initialize a newly created student object with the passed in value
strncpy(id, initId, Student::MAX_CHAR - 1);
if (Student::MAX_CHAR > 0)
{
id[Student::MAX_CHAR - 1] = '\0';
}
}
It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
In your less than you are doing strcmp(id, aStudent.id) > 0.
Basically a "greater-than"
So s1.isLessThanByID(s2) is false.
> To fix it, I had to add in #include <cstring> on my student.h file
1- include what you need. If you are going to use functions declared in the `cstring' header, then you should include it.
a- your header does not use `cstring', your source does. `student.cpp' is the one that should include it.
Just want to make sure I am understanding correctly, so for that portion, strcmp(id, aStudent.id) < 0, (if less than 0) I should return false else return true correct? Just want to make sure I have that understood correctly.
I've also placed the cstring into student.cpp, which it then compiles with no issues still. However the ouput is still incorrect. In windows the output is;
1 2
G10 3.9
G20 3.5
But as soon as I run it in Linux, the output is now;
1 2
G10 0
G20 6.95263e-310
Which is where I am really confused on where the disconnect is between the two systems.
#include "student.h"
#include <cstring>
//implement the required 3 functions here
Student::Student(constchar initId[], double gpa) : gpa(gpa)
{
// initialize a newly created student object with the passed in value
strncpy(id, initId, Student::MAX_CHAR - 1);
if (Student::MAX_CHAR > 0)
{
id[Student::MAX_CHAR - 1] = '\0';
}
}
bool Student::isLessThanByID(const Student& aStudent) const
{
// compare the current student object with the passed in one by id.
if (strcmp(id, aStudent.id) < 0)
{
returnfalse;
}
else
{
returntrue;
}
}
bool Student::isLessThanByGpa(const Student& aStudent) const
{
// compare the current student object with the passed in one by gpa
if (gpa < aStudent.gpa)
{
returntrue;
}
else
{
returnfalse;
}
}
void Student::print() const
{
cout << id << '\t' << gpa << endl;
}
talking about obfuscation.
you are basically doing return not (strcmp(id, aStudent.id) < 0); which is equivalent to return strcmp(id, aStudent.id) >= 0;
if `this->id' is greater or equal to `b.id'
If you want lexicograph order then simply do return strcmp(id, aStudent.id) < 0;
I've changed that part and of course the pause won't work in Linux, however are you getting the correct output for the gpa? I am not still for some reason. I am still getting;
if we get different errors or output, then the code apparently does not correspond to the actual code you're having problems with, and consequently analyzing it is pointless.
Don't post code which is different from your own but does not reproduce the problem ( or has other problems )
> Perhaps it is just my system
unlikely.
There are a lot of online services that will let you run your code if you want to try.
get a debugger and perform an step-by-step run. Inspect your variables and see if they sudenly change its values (by instance, because of an out-of-bounds access)
Also, make sure that you are actually running that code and not an older version.