Weird characters in code

Hello,

I am currently working on a lab assignment and when running the program, I get multiple weird characters within it. Is there something that I am missing or doing wrong? I know I am not so sure about the boolan functions to compare in student.cpp. Thank you.


student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

  #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
#include "student.h"


//implement the required 3 functions here


Student::Student(const char initId[], double gpa)
{
	// initialize a newly created student object with the passed in value
	
	cout << "Id: " << initId << " GPA: " << gpa << endl;

	
}

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
Your id and gpa members are never initialized and contains garbage.
I see, in the void Student::print() const section. For initialization it looks I have to do so in the Student::Student section. To get it to talk, would a pointer be better? (That's what we are learning about, so I assume so,but I don't want to assume). I cannot edit the print() section as I have to implement using the other sections.

Hopefully this makes sense. Still learning here and trying to get help without getting the answers.
I was able to get the Id through by using;

 
strcpy_s(id, initId);


However the gpa part still can't get working. I tried using the strcpy_s again for that but it doesn't like it.
closed account (48T7M4Gy)
Hint: gpa is a number not a c-string
I was able to get this now. Thank you!
Topic archived. No new replies allowed.