Help with small project

hey pepole i will be glad if someone can direct me if iam doing it good
iam not asking the slution just tips to make a good program .

i need to make 2 class Surviver Tribe.
in surviver class


Surviver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int const size = 20;
class Surviver{
private:
	char *name[size];
	int age;
	double Bw; //begin weight
	double We; //end weight
public:
	Surviver(char* name, int a, double e);
	void CheckWeight(double w);
	void print()const;
	~Surviver();
};


surviver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include"Surviver.h"
Surviver::Surviver(char *name, int a, double w){
	name = new char[20];
	age = a;
	Bw = w;
	We = -1.0;
}
Surviver::~Surviver(){
	delete[]name;


}
void Surviver::CheckWeight(double w){
	We = w;
}
void Surviver::print()const{
	cout << "Name: " << name << " Weight begin " << Bw << endl;
}


the tribe class iam working on it and ask later. sorry just want to see if iam doing it in a good way or i have mistakes(new to c++)
closed account (48T7M4Gy)
Small point, Surviver is spelt Survivor.

Suggest you use <string>'s instead of char*.
Use meaningful variable names.
What is CheckWeight supposed to do. Maybe call it CheckWeightChange()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using namespace std;
int const size = 20;
class Survivor{
private:
	string name;
	int age;
	double begin_weight;

public:
	Survivor(string, int , double );
	void CheckWeight(double );
	void print()const;
	~Survivor();
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include"Survivor.h"

Survivor::Survivor(string aName, int aAge, double aBeginWeight){
	name = aname;
	age = aAge;
	begin_weight = aBeginWeight;
}

Survivor::~Survivor(){}

void Survivor::CheckWeight(double aCurrentWeight){
	/* ??? */
}

void Survivor::print()const{
	cout << "Name: " << name << " Weight begin " << begin_weight << endl;
}
Last edited on
ok i will explain better sorry for misunderstood.
in class Surviver i need to do
in header file:
-static string with 20 size.
-age
-weight at the begain
-weight at the end;

the methods in this class is:
-first methos that recive name age and the begain weight and update and putt in end weight=-1
-second methos is to update end weight ,method recive parameter and update the end weight.
-print method print the name and the begining weight;

surviver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string>
using namespace std;
int const size = 20;
class Surviver{
private:
	char name[size];
	int age;
	double Bw; //begin weight
	double We; //end weight
public:
	Surviver(char n, int a, double e);
	void CheckWeight(double w);
	void print()const;
	~Surviver();
};

Surviver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include"Surviver.h"
Surviver::Surviver(char n[20], int a, double w){
	for (int i = 0; i < 20; i++){
	name[i] = n[i];
}
	age = a;
	Bw = w;
	We = -1.0;
}
Surviver::~Surviver(){
	delete[]name;


}
void Surviver::CheckWeight(double w){
	We = w;
}
void Surviver::print()const{
	cout << "Name: " << name << " Weight begin " << Bw << endl;
}

Last edited on
closed account (48T7M4Gy)
OK, here is how you handle char[] instead of <string>, plus a small test. No problem with CheckWeight, it's your program, so it's up to you how/what functionality you have. :)

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
#include <iostream>
using namespace std;

class Survivor{
private:
    char name[20];
    int age;
    double begin_weight;
    
public:
    Survivor(char *, int , double );
    double CheckWeightChange(double );
    void print()const;
    ~Survivor();
};

Survivor::Survivor(char *aName, int aAge, double aBeginWeight){
    strcpy(name, aName);
    age = aAge;
    begin_weight = aBeginWeight;
}

Survivor::~Survivor(){}

double Survivor::CheckWeightChange(double aCurrentWeight){
    return aCurrentWeight - begin_weight;
}

void Survivor::print()const{
    cout << "Name: " << name << " Weight begin " << begin_weight << endl;
}

int main(){
    Survivor man( "Bob", 22, 15);
    man.print();
    
    cout << "Weight difference " << man.CheckWeightChange(20) << endl;
    return 0;
}
Topic archived. No new replies allowed.