Dynamical Char Arrays

I had a question concerning the use of dynamic char arrays with the constructor of a class. Here, I have a class in which I pass a name that will be stored in a dynamically created char array. Since the array size can vary depending on the name passed to the constructor, I am not sure what steps to use to allocate the correct array size. I wish to avoid using string and the STL. Here is what I have so far and it's almost complete except for the name.

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
/* 
 * Employee.h
 *
 *  Created on: Feb 11, 2012
 *      Author: LEC
 */
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include <string>

using std::string;

class Employee
{

public:
	Employee();
	~Employee();
	Employee(const char *, int, int);
	void evaluate ();
	double computeEvaluationAverage();


private:
	const char *m_name;
	int m_salary;
	int m_numOfEvaluations;
	int *m_evaluations;

public:

	// mutators

	// accessors
	int getNumOfEvaluations();
	int getSalary();
	string getName();
};

#endif 


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Employee.cpp
 *
 *  Created on: Feb 11, 2012
 *      Author: lec
 */
#include <iostream>
#include <string>
#include <cstdlib>
#include "Employee.h"

using namespace std;

// default constructor
Employee::Employee()
{

} /* end default constructor */

Employee::Employee(const char * Name = "Employee_Name", int Salary = 0, int numOfEvaluations = 0)
{

m_name = Name;
m_salary = Salary;
m_numOfEvaluations = numOfEvaluations;

} /* end Employee(char *, int, int) */

Employee::~Employee()
{
	string deleteEmployee("deleting employee ");
	cout << deleteEmployee << getName() << endl;

	// clean up
	delete [] m_name;
	m_name = NULL;

	delete [] m_evaluations;
	m_evaluations = NULL;
}

void Employee::evaluate()
{
	m_evaluations = new int [getNumOfEvaluations()];

	for (int i = 0; i < getNumOfEvaluations(); ++i)
	{
		m_evaluations[i] = rand()%101;
	} /* for */

} /* end evaluate() */

double Employee::computeEvaluationAverage()
{
	double sum = 0;

	for (int i = 0; i < getNumOfEvaluations(); ++i)
	{
		sum += m_evaluations[i];
	}

	return (sum / getNumOfEvaluations());

} /* end computeEvaluationAverage() */

// Accessors and Mutators

string Employee::getName()
{
	string name = m_name;
	return name;
} /* end getName() */

int Employee::getSalary()
{
	return m_salary;
} /* end getSalary */

int Employee::getNumOfEvaluations()
{
	return m_numOfEvaluations;
} /* end getNumOfEvaluations */


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
/*
 * Driver.cpp
 *
 *  Created on: Feb 11, 2012
 *      Author: lec
 */
#include <iostream>
#include <cstdlib>
#include <string>

#include "Employee.h"

using namespace std;

int main ()
{
	srand(time(0));

	char name[] = "John Smith";
	int salary = rand();
	int evaluations = rand()%10;

Employee firstEmployee(name, salary, evaluations);

firstEmployee.evaluate();
cout << firstEmployee.computeEvaluationAverage() << endl;

	return 0;
} /* end main */


Thanks in advance,

L
Last edited on
Alright, first thing is first: Don't you dare call yourself L. I will write your name down, for you see, I have the eyes; I can see your name above your post. (Seriously though, it's stupid)

Anyway, why on Earth would you want to avoid the ease of strings? The STL too? WHY!?

I cannot think of any way to just take the number of elements from an array. You'd have to use something like vectors, or strings.


What the hell? Remove strings is one thing, but to put an end to things like iterators, no.
strlen is what you want.

1
2
3
4
5
const char foo[] = "whatever";
int foolen = strlen( whatever );

char* bar = new char[ foolen + 1 ];  // add 1 for the null terminator!
strcpy( bar, foo );  // copy foo to bar 



I wish to avoid using string and the STL


May I ask why? No point in making things harder on yourself.
As part of an algorithm class, we are studying how someone might create an algorithm to do, for example, what a string might do. It is the instructor's request NOT to use string and the STL and I quote, "...if you do you will get zero points." Sounds like a good enough reason to me.
Last edited on
Thanks Disch, that worked great.
Topic archived. No new replies allowed.