help me with my homework T T

i lost all my hope in solving it ..please help me
ps: i can only use iostream , stdio.h and i think i can use strcpy

1. Your task is to create the required C++ classes with inheritance relation[Person class, and Athlete class].
2. You should build header file and source file for each class (i.e. .h and .cpp).
3. You should use the header guard or pragma once to prevent multiple includes.
4. Each class should have constructor and destructor.
5. Each class should have setter and getter functions for all member variables (e.g. setFirstName(char fn[10]); getFirstName();)

In the main you should do the following: 1. Read data from a given file called “data.txt” and save it in an array of Athlete. Make sure the size of array should fit the count of Athletes in the file

File format : The first line in the file contains number of records and each other line in the file represents a data of an athlete so that white spaces separate the first name, last name, nationality, and the distance.

# of records/lines
Damar Forbes Jamaica 7.79

2. Write function that takes a reasonable distance and the array of Athlete and print out in a file namelly “out.txt” the names,nationalities and the distances of the athletes who exceeded a given distance by the user. [Just a Note: The record for men is 8.95 meter and for women is 7.52 meter]


Hello strangerone,

Welcome to the forum,

When I see instructions like this the first thing I do is break it up so it looks like smaller steps.


P.S.: i can only use iostream , stdio.h and i think i can use strcpy.

1. Your task is to create the required C++ classes with inheritance relation[Person class, and Athlete class].

2. You should build header file and source file for each class (i.e. .h and .cpp).

3. You should use the header guard or pragma once to prevent multiple includes.

4. Each class should have constructor and destructor.

5. Each class should have setter and getter functions for all member variables (e.g. setFirstName(char fn[10]); getFirstName();)


In the main you should do the following:

1. Read data from a given file called “data.txt” and save it in an array of Athlete. Make sure the size of array should fit the count of Athletes in the file

File format : The first line in the file contains number of records and each other line in the file represents a data of an athlete so that white spaces separate the first name, last name, nationality, and the distance.

# of records/lines
Damar Forbes Jamaica 7.79

2. Write function that takes a reasonable distance and the array of Athlete and print out in a file namely “out.txt” the names,nationalities and the distances of the athletes who exceeded a given distance by the user. [Just a Note: The record for men is 8.95 meter and for women is 7.52 meter]

Now when you see each number it is just one small step or part of the program.

Start with number 1 and create your classes. I would start with a short version of "main" to allow you to compile the program.

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "\n Press Enter to continue: ";
    std::cin.get();

    return 0;

It looks like this assignment is not only learning "classes", but also creating multiple files for the project.

Now it is your turn to come up with some code and post it. It may or may not be right, but it is a place to start from.

I will say this now in hopes it will not be needed later:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


When you post your code also include the input file or at least a good sample, for large files, and any error messages that you received when you compiled the program. It is also best to copy the entire error message not give your idea of what it is.

It also helps to know what IDE and compiler you are using.

Hope that helps,

Andy
thanks for Replying i thought no one will reply <3
I tried to write the person and Athlete classes

person.h
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
#pragma once
#include <stdio.h>
#include <string.h>
class person
{
public:
	person();
	~person();
	
	void set_firstname(char fn[15]);
	char* get_firstname();

	
	void set_lastname(char ln[15]);
	char* get_lastname();

	
	void set_nationality(char nat[15]);
	char* get_nationality();

	
private: 
	char firstname[15];
	char lastname[15];
	char nationality[15];
};


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


person::person()
{
}


person::~person()
{
}


void person::set_firstname(char fn[15])
{
	strcpy(firstname, fn);

}
char* person::get_firstname()
{
	return firstname;
}
void person::set_lastname(char ln[15])
{
	strcpy(lastname, ln);
}
char* person::get_lastname()
{
	return lastname;
}
void person::set_nationality(char nat[15])
{
	strcpy(nationality,nat);
}
char* person::get_nationality()
{
	return nationality;
}


Athlete.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include "person.h"
class athlete :
	public person
{
public:
	athlete();
	~athlete();
	void set_distance(char d[10]);
	char* get_distance();

private: char distance[10];
}; 


Athlete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "athlete.h"



athlete::athlete()
{
}


athlete::~athlete()
{
}

void athlete::set_distance(char d[10])
{
	strcpy(distance, d);

}
char* athlete::get_distance() {
	return distance;
}

is it right so far?

i use visual studio 2015

and this is a sample of the data file
40
Aleksandr Menkov Russia 8.09
Aleksandr Petrov Russia 7.89
Alyn Camara Germany 7.72
Arsen Sargsyan Armenia 7.62
Boleslav Skhirtladze Georgia 7.26
Christian Reif Germany 7.92


Hello strangerone,

You are right. With your OP (original post) few if any will be willing to do your work for you.

In the "person.h" file the "#pragma once" is nice, but you should learn the include guard.

Something like:
1
2
3
4
5
6
#ifndef _PERSON.h_
#define _PERSON.h_

// <--- Your code here.

#endif  // !_PERSON.h_ 

Next the lines:
1
2
#include <stdio.h>
#include <string.h> 

Should not be in a header file. In the future this could cause big problems. These includes should be in a ".cpp" file.

Looking at these header files makes me think that this is a C program not a C++ program. "class"es are C++ and not available to C programs.

Although you can mic C++ and C code it is not a good idea. With your version of VS 2015 I guarantee "strcpy()" will be a problem in a C++ program.

As I look at the header files I see C style strings (character arrays). Are you stuck with using these or can you use the "std::string", header file "<string>", which would work better and you would not nee the C function "strcpy()".

The public section looks alright, but I am a bit rusty on C code I will need to compile and test the code.

In "person.cpp" The first lines you are missing are the include files that are in "person.h". The ".cpp" file needs to have them here not from a header file like "person.h".

In the default ctor I would do something to give the C strings a value instead of the garbage that is there.

The rest of the functions look OK for now. I will know more when I compile and test the functions.

In "Athlete.h" I would make "distance" a "double" since it is a number. As a character array you will fine it difficult to use in calculations.

In "Athlete.cpp" you will need to also have it deal with a "double".

In the ctor this is a good place to give "distance" a value before it is used.

I too use VS (Visual Studio). Just upgraded to the 2017 version, but still have the 2015 version.

Thank you for the input file. That is a big help so everyone can use the same information and see what output differs.

I will load everything up and test. I will let you know what I find.

Hope that helps,

Andy


A constructor like this, left blank, does not need to be defined. You already get a blank constructor by default.

1
2
3
person::person()
{
}


Instead, make a constructor that takes data as arguments and fills in your data containers.
Last edited on
Example...

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
#include <iostream>
#include <string>

using namespace std;

class person {
	string first_name;
	string last_name;
	string country;

public:
	person(string f, string l, string c) {
		first_name = f;
		last_name = l;
		country = c;
	}

	string get_name() {
		return first_name + " " + last_name;
	}

};

int main()
{
    
    
   person one("John", "Doe", "USA");

    cout << one.get_name();
    cin.get();

    return 0;
}
Last edited on
So I am noticing a problem with inheritance.
If you want athlete to be a child of person and you define a constructor for person, then you will also have to define a default constructor for person. Also, in order for athlete to inherit members form person, they will need to be changed to protected. Again another example...

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
class person {
protected: //must be protected not private
	string first_name;
	string last_name;
	string country;

public:
	//default constructor
	person() {
	};

	//constructor
	person(string f, string l, string c) {
		first_name = f;
		last_name = l;
		country = c;
	}

	string get_name() {
		return first_name + " " + last_name;
	}

};

class athlete : public person {
	double distance;
public:
//constructor for athlete
	athlete(string f, string l, string c, double d) {
		first_name = f;
		last_name = l;
		country = c;
                distance = d;
	}

};
Last edited on
You don't have to create a default constructor for person.

As in the following code, the constructor for the child class can call a specific constructor of the parent. The child class doesn't need the parent's variables to be public or protected; they can stay private and the child can access them through whatever accessors the parent provides.

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
52
53
#include <iostream>
#include <string>

using namespace std;

class person {
private:
	string first_name;
	string last_name;
	string country;

public:

	//constructor
	person(string f, string l, string c) {
		first_name = f;
		last_name = l;
		country = c;
	}
	
	virtual void outputData()
	{
	    cout << first_name << " " << last_name << " " << country;
	}

	string get_name() {
		return first_name + " " + last_name;
	}

};

class athlete : public person {
	double distance;
public:
//constructor for athlete
	athlete(string f, string l, string c, double d) : person (f, l, c)
	{
                distance = d;
	}
	
	void outputData()
	{
	    person::outputData();
	    cout << " " << distance;
	}

};

int main()
{
  athlete an_athlete("Mike","Runner", "RunnerLand", 10.2);
  an_athlete.outputData();
}

Last edited on
Next the lines:

1
2
#include <stdio.h>
#include <string.h>  


Should not be in a header file. In the future this could cause big problems. These includes should be in a ".cpp" file.


What? There is nothing wrong with #including header files in a header file, if something from those headers is required inside the header file. However since nothing in that header require anything from those headers you are correct that the includes should not be in that header. You should #include the header in every file that requires the headers not rely on compiler magic to include a required include.

Looking at these header files makes me think that this is a C program not a C++ program. "class"es are C++ and not available to C programs.

The use of classes is one of the reasons that this is a C++ program not a C program.

Although you can mic C++ and C code it is not a good idea.

The problem here is not mixing C and C++ code, the problem is that the OP is using horrible C strings. If you use C-strings you will need to use those C-string functions.

With your version of VS 2015 I guarantee "strcpy()" will be a problem in a C++ program.

But note that this is a Visual Studio problem, not a C++ program. The standard C string functions like "strcpy()" are perfectly legal functions in C++, no matter what Visual Studio may say. By the way all you need to do is follow the instructions in the error message to disable these bogus warning/error messages to be good to go.

Since the OP is using C-strings he really needs to make sure he properly const qualifies those return values. For example:

1
2
3
4
5
6
7
class person
{
...
public:
...
	void set_firstname(char fn[15]);
	char* get_firstname();


The last line above should be: const char* get_firstname();

Also that set_firstname() function should insure that the string passed into the function doesn't overflow the firstname[] member variable. Remember all you get when you pass a C-string into a function is a pointer, this pointer doesn't have any idea as to the size of the string being passed into the function. This is one of the best reasons to be using a std::string. A std::string always knows it's size and it will automatically grow to the proper size.



thanks for you all !!
Unfortunately I'm not allowed to use string library so i have to use an array of char
i didn't thought that constructors are important because I'm reading from a file put i added them now
With your version of VS 2015 I guarantee "strcpy()" will be a problem in a C++ program.

it give me warnings but as long as i can use it it's fine with me


i don't know if i did it right
person.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
#include "person.h"
#include <stdio.h>
#include <string.h>

person::person(char fn[20],char ln[20], char nat[20] )
{
	strcpy(firstname,fn);
	strcpy(lastname,ln);
	strcpy(nationality,nat);
}


person::~person()
{
}


void person::set_firstname(char fn[20])
{
	strcpy(firstname, fn);

}
const char* person::get_firstname()
{
	return firstname;
}
void person::set_lastname(char ln[20])
{
	strcpy(lastname, ln);
}
const char* person::get_lastname()
{
	return lastname;
}
void person::set_nationality(char nat[20])
{
	strcpy(nationality, nat);
}
const char* person::get_nationality()
{
	return nationality;
}


athlete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "athlete.h"



athlete::athlete(double d, char fn[20], char ln[20], char nat[20]) : person( fn, ln,  nat)
{
distance = d;
}


athlete::~athlete()
{
}

void athlete::set_distance(double d)
{
	distance = d;
}
double athlete::get_distance() {
	return distance;
}


i tride to write the main too
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
int main() {
	FILE *pff;
	athlete *save;
	char A[20];
	int num;
	double n;

	pff = fopen("D:\\files\\data.txt", "r");
	fscanf(pff, "%s",&A);

	num = atoi(A);

	save = new athlete[num];

	for (int i = 0; i < num; i++)
		for (int j = 0; j < 4; j++)
		{
			if (j = 0)  fscanf(pff," %s",save[j].set_firstname);
			if (j = 1)  fscanf(pff, " %s", save[j].set_lastname);
			if (j = 2)  fscanf(pff, " %s", save[j].set_nationality);
			if (j = 3)  fscanf(pff, " %f", save[j].set_distance);
		}
			
	


	
	 
	return 0;
}

void func(double n, athlete *save, int num)
{
	FILE *pp;
pp = fopen("D:\\files\\out.txt", "w");

for (int i = 0; i <num; i++)
{
	if (atof(save[i].get_distance) > n)
		fprintf(pp, "%s %s %s %f", save[i]);
}

}


in the main i read from a file then put the information into an array of athlete and Write function that takes a reasonable distance and the array of Athlete and print out in a file namelly “out.txt” the names,nationalities and the distances of the athletes who exceeded a given distance by the user.

but there is an error when i tride to try it
person::set_firstname non standard syntax use & to create a pointer to member
and the same error for lines 19 20 21 and 39
You can't use set_XXX() with fscanf(). The fscanf() function expects a variable of the correct type, in this case a C-string not a function.

Using C-strings is going to make this assignment much harder since you need to insure that you don't overflow your strings.

i don't know if i did it right

You don't know if you did what right?

Do you realize that the following are all the same to the compiler?

1
2
3
4
5
6
void set_name(char name[]);
void set_name(char name[1000]);
void set_name(char name[10]);
void set_name(char* name);
// and when used in a function prototype
void set_name(char []);


Your code, at present, is very susceptible to overflowing your strings, you need to check to insure that doesn't happen. And since those strings are inside the class you should be checking inside the class member functions that "set" the names.

Also in your athlete() constructor why are the sizes shown in your prototype different than the sizes in the person class?

then what should i use instead of fscanf? it is the only way i can think of it to solve the problem

Do you realize that the following are all the same to the compiler?


iknow this now ...sorry for my stupidity <\3

Also in your athlete() constructor why are the sizes shown in your prototype different than the sizes in the person class?

i adjusted it so i wont have problems with overflowing

ps: sorry about my language....i'm not a native speaker
i adjusted it so i wont have problems with overflowing

How? Are you checking the length of the incoming string to insure it is small enough to fit into your string?

Topic archived. No new replies allowed.