Different output in Linux

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.

student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <cstring>
using namespace std;

class Student
{
public:
	Student(const char initId[], double gpa);
	bool isLessThanByID(const Student& aStudent) const;
	bool isLessThanByGpa(const Student& aStudent) const;
	void print()const;
private:
	const static int MAX_CHAR = 100;
	char 	id[MAX_CHAR];
	double	gpa;
};
#endif 


student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "student.h"


//implement the required 3 functions here


Student::Student(const char 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)
	{
		return true;
	}
	else
	{
		return false;
	}
	
	
}

bool Student::isLessThanByGpa(const Student& aStudent) const
{
	// compare the current student object with the passed in one by gpa
	if (gpa < aStudent.gpa)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

void Student::print() const
{
	cout << id << '\t' << gpa << endl;
}


app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "student.h"

int main()
{
	Student s1("G10", 3.9);
	Student s2("G20", 3.5);

	s1.print();
	s2.print();

	if(s1.isLessThanByID(s2))
	{
		cout << "about right!" << endl;
	}
	else
	{
		cout << "uhmm ..." << endl;
	}
	if(!s1.isLessThanByGpa(s2))
	{
		cout << "about right!" << endl;
	}
	else
	{
		cout << "uhmm ..." << endl;
	}

	//system("pause");
	return 0;
}
Last edited on
Are you saying it compiles with strncpy_s in your code? Shouldn't it take four arguments? As far as I know this function is not part of standard C++.
Sorry, in windows I had to have the _s added. Once transferred I removed that _s to help it compile.
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(const char 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';
	}
	
	
	
}
http://linux.die.net/man/3/strcmp
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.
Post your actual current code.
student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
using namespace std;

class Student
{
public:
	Student(const char initId[], double gpa);
	bool isLessThanByID(const Student& aStudent) const;
	bool isLessThanByGpa(const Student& aStudent) const;
	void print()const;
private:
	const static int MAX_CHAR = 100;
	char 	id[MAX_CHAR];
	double	gpa;
};
#endif 


student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "student.h"
#include <cstring>

//implement the required 3 functions here


Student::Student(const char 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)
	{
		return false;
	}
	else
	{
		return true;
	}
	
	
}

bool Student::isLessThanByGpa(const Student& aStudent) const
{
	// compare the current student object with the passed in one by gpa
	if (gpa < aStudent.gpa)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

void Student::print() const
{
	cout << id << '\t' << gpa << endl;
}


app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "student.h"

int main()
{
	Student s1("G10", 3.9);
	Student s2("G20", 3.5);

	s1.print();
	s2.print();

	if(s1.isLessThanByID(s2))
	{
		cout << "about right!" << endl;
	}
	else
	{
		cout << "uhmm ..." << endl;
	}
	if(!s1.isLessThanByGpa(s2))
	{
		cout << "about right!" << endl;
	}
	else
	{
		cout << "uhmm ..." << endl;
	}

	system("pause");
	return 0;
}


which gives
G10	3.9
G20	3.5
uhmm ...
about right!
sh: pause: command not found



1
2
3
4
5
6
7
8
	if (strcmp(id, aStudent.id) < 0)
	{
		return false;
	}
	else
	{
		return true;
	}
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;

1
2
3
4
G10     0
G20     6.95291e-310
about right!
uhmm ...


Perhaps it is just my system or something..
> I've changed that part
http://www.eelis.net/iso-c++/testcase.xhtml (6)
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.


http://www.cplusplus.com/forum/articles/40071/#msg216313
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.
Topic archived. No new replies allowed.