loop through pointer array

I am doing a memory allocation exercise using a pointer array. The task is to create an array of 26 objects. Then loop through the alphabet and for each item in the array, use a setName() method to set a name, (e.g. name each item with each letter of the alphabet and churn it out), for the alphabet/object at that index in the array.

Then loop through the array again with another loop and print all the names (with a getName() method). I've already written the code (OO). I can't figure out the moment what the error is in my code. I know there are easier ways to cout the alphabet by, for example, just using a for loop, but I'm trying to see how a loop and a pointer array work together and the use of constructors and destructors. It is creating more than 40 objets, and they are all being called 'a'; somehow the loop is not working and producing only 26 objects and calling each one by a different letter of the alphabet. Can anybody help and tell me what the bug(s) is/are?

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

using namespace std;

class Alphabet {
private:
	string name;
public:
	Alphabet() {
		cout << "Alphabet created" << endl;
	}
	Alphabet(const Alphabet& other) :name(other.name) {
		cout << "Alphabet created by copying" << endl;
	}
	~Alphabet() {
		cout << "destructor called" << endl;
	}

	void setName(string name) {
		this->name = name;
	}

	void speak() const {
		cout << "My name is: " << name << endl;
	}
};


  int main()
{
	const char ALPH = 'a';
	string name(6, ALPH);
	Alphabet* alphabet1[ALPH];

	for (int i = 0; i<ALPH; i++) {

		alphabet1[i] = new Alphabet;
		alphabet1[i]->setName("a");
		alphabet1[i]->speak();
	}
	
	for (int i = 0; i<ALPH; i++) {

		delete alphabet1[i];
	}
return 0;
}
Last edited on
You are using ALPH, which is a char, in the loop as if it were an integer. The character 'a' has the code 40, so the loop goes 40 times. In the loop you are explicitly setting the new alphabet objects the value "a", so they are always going to be "a".
you want to set the loop so it only goes 26 times like so:
1
2
3
for(int i = 0; i < 26; ++i)
{
}

You also want to declare the Alphabet array with 26.
Thanks man, it's improved, it's spitting out the 26 objects, although I need to figure out what to replace "a" in line 35 with so it loops through each letter of the alphabet.
Perhaps my setName method needs tinkering with.
Last edited on

I need to figure out what to replace "a" in line 35 with so it loops through each letter of the alphabet.


1
2
3
4
constexpr int noOfLetters{ 26 };
for ( int i{ 'a' }; i < 'a' + noOfLetters; i++ ) {
	std::cout << static_cast<char>( i ) << "  ";
}
Thanks, I know you can use it that way. In fact I thought of using:

int counter = 0;

for (char n = 'a'; n <= 'z'; c++) {

cout << n << endl;
counter++;
}

What I mean is I want to modify my loop so I can still use "alphabet1[i] = new Alphabet;"/a pointer array. That's the point of the exercise I'm working on, namely, combine a pointer array with a loop and methods to produce the alphabet. Thanks to all of you out there for taking the time to help. I'll keep trying. If I succeed before anybody else I'll share the answer in case someone is interested.
pabs8 wrote:
That's the point of the exercise I'm working on, namely, combine a pointer array with a loop and methods to produce the alphabet.
But...
pabs8 wrote:
The task is to create an array of 26 objects.


which suggests an array of objects and not pointers-to-objects (and objects should be preferred over pointers-to-objects unless there's a specific need for pointers.)

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    const std::size_t numLetters = 26;
    std::string letters = "abcdefghijklmnopqrstuvwxyz";

    Alphabet alphabet[numLetters];

    for (std::size_t i = 0; i < numLetters; ++i)
        alphabet[i].setName(std::string(1, letters[i]));

    for (auto& a : alphabet)        // C++11 required, otherwise loop as above.
        a.speak();
}


Last edited on
Thanks cire, that was awesomely good, it really helped me. I successfully ran my little program. The argument (1, letters[i]) really nailed it. Thanks for your time and thanks to integralfx and phanalax as well.
Last edited on
Topic archived. No new replies allowed.