Need help with std::array and class

I'm a newbie to C++ programming. Need some help working with STD::arrays. I'm trying to pass the values of an array to a function, then display the value. Please see code below. So far i am able to pass the value to getName function but nothing gets pass to setName. Any help is greatly appreciated.

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

using namespace std;

// Class Records
class Records
{
public:
	// Constants size of array
	static const size_t students = 20;
	Records(std::array< std::string, students> &);
	void setName(std::string &); 
	std::string getName();
	void displayMessage();

private:
	std::array< std::string, students > employee;
	std::string fullName;
};

// Constructor initialize
Records::Records( array< string, students> &name)
	:employee(name)
{
}

void Records::setName( string &name)
{
	fullName = name;
}

string Records::getName()
{
	return fullName;
}

void Records::displayMessage()
{
	cout << "what is your name? " << endl;
	cout << getName()
		<< "\n" << endl;
}

int main()
{
	const size_t num = 20;
	array< string, num > one = {"Mike Johnson Hary Da"}; 
	array< string, num > test = {"Hary Da Johnson Mike"};

	Records studentone(one);
	Records studenttwo(test);
	
	for ( size_t i = 0; i < num; ++i)
	studentone.setName(one[i]);

	for ( size_t n = 0; n < num; ++n)
	studenttwo.setName(test[n]);

	studentone.displayMessage();
	studenttwo.displayMessage();
	

	return 0;
}
Here's what is happening in this code:

1
2
3
        const size_t num = 20;
	array< string, num > one = {"Mike Johnson Hary Da"}; 
	array< string, num > test = {"Hary Da Johnson Mike"};


You are creating two array type data variables "one" and "test", that is supposed to have have 20 strings. However, you have given a default value for the first element in only in both variables. So, both "one" and "test" have element on 0th index, and rest of 19 indices have empty string.
1
2
	Records studentone(one);
	Records studenttwo(test);

You are then creating records using those variables "one" and "test".
1
2
3
4
5
6
7
8
	for ( size_t i = 0; i < num; ++i)
	studentone.setName(one[i]);

	for ( size_t n = 0; n < num; ++n)
	studenttwo.setName(test[n]);

	studentone.displayMessage();
	studenttwo.displayMessage();

In the for loop, you are looping 20 times, for both "studentone" and "studenttwo" and setting values of "one" and "test" instances private string variable "fullName" 20 times using variable "one" and "test" in main function. However, "one" and "test" both contain empty string for last 19 indices. So, display message will display the last element that was set, which is basically an empty string.

Hope that helps.
Yes. that helps a lot.
Thanks for breaking down the code.
Topic archived. No new replies allowed.