Filling a dynamically allocated array with values/strings

I need to write a program that dynamically allocates an array of a size input by the user and fill it with std::strings. It has to then sort the array. So I want to know how to fill a dynamically allocated array of std::strings.

Thanks in advance.
http://www.cplusplus.com/doc/tutorial/dynamic/

You fill the array the exact same way you would with a non-dynamically-allocated array.

Help me out starting from here:

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
#include <iostream>
#include <string>
#include "keep_window_open.h"

int getSizeInput();
std::string& getStringArrayInput(std::string nameArray[], const int size);

int main()
{
	using namespace std;
	cout << "How many names do you wish to enter? ";
	int size = getSizeInput();

	cout << "Please enter " << size << " names (first names only): ";
	string *nameArray = new string[size];
	*nameArray = getStringArrayInput(nameArray, size);

	keep_window_open();
	return 0;
}

int getSizeInput()
{
	using namespace std;
	int size;
	cin >> size;
	cin.ignore(32767, '\n');
	return size;
}

std::string& getStringArrayInput(std::string nameArray[], const int size)
{
	using namespace std;
	for (int count = 0; count < size; ++count)
	{
		cin >> nameArray[count];
		cin.ignore(32767, '\n');
	}
	return nameArray[size];
}


For taking input for the array, is what I have fine or did I do it wrong? If it's wrong, help me fix it please. Or it would be better to just take the input for it in main() instead?

And in the meantime, I'll be reading the info in that link you provided.
Last edited on
Yes. But you don't return the array. Arrays are not sent by value like variables, so if it is changed in a function it will be changed everywhere.

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

int getSizeInput();
void getStringArrayInput(std::string nameArray[], const int size);

int main()
{
	using namespace std;
	cout << "How many names do you wish to enter? ";
	int size = getSizeInput();

	cout << "Please enter " << size << " names (first names only): ";
	string *nameArray = new string[size];
    getStringArrayInput(nameArray, size);


	return 0;
}

int getSizeInput()
{
	using namespace std;
	int size;
	cin >> size;
	cin.ignore(32767, '\n');
	return size;
}

void getStringArrayInput(std::string nameArray[], const int size)
{
	using namespace std;
	for (int count = 0; count < size; ++count)
	{
		cin >> nameArray[count];
		cin.ignore(32767, '\n');
	}
	
}
Got it. I'll just take the input for the array in main(), then.

How do I get this to work for a string array, though?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void selectionSort(std::string nameArray[], const int size)
{
	for (int startIndex = 0; startIndex < size; ++startIndex)
	{
		int smallestIndex = startIndex;

		for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			
			if (nameArray[currentIndex] < nameArray[smallestIndex])
				
				smallestIndex = currentIndex;
		}

		std::swap(nameArray[startIndex], nameArray[smallestIndex]);
	}
}


I displayed the array after doing this and it wasn't sorted at all.

Here's the whole code:

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
#include <iostream>
#include <string>
#include "keep_window_open.h"

int getSizeInput();
void selectionSort(std::string nameArray[], const int size);
void displayArray(const std::string nameArray[], const int size);

int main()
{
	using namespace std;
	cout << "How many names do you wish to enter? ";
	int size = getSizeInput();

	cout << "Please enter " << size << " names (first names only): ";
	string *nameArray = new string[size];
	for (int count = 0; count < size; ++count)
	{
		cin >> nameArray[count];
		cin.ignore(32767, '\n');
	}

	for (int index = 0; index < size; ++index)
	{
		cout << nameArray[index] << "\n";
	}

	selectionSort(nameArray, size);


	keep_window_open();
	return 0;
}

int getSizeInput()
{
	using namespace std;
	int size;
	cin >> size;
	cin.ignore(32767, '\n');
	return size;
}

void selectionSort(std::string nameArray[], const int size)
{
	for (int startIndex = 0; startIndex < size; ++startIndex)
	{
		int smallestIndex = startIndex;

		for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			
			if (nameArray[currentIndex] < nameArray[smallestIndex])
				
				smallestIndex = currentIndex;
		}

		std::swap(nameArray[startIndex], nameArray[smallestIndex]);
	}
}

void displayArray(const std::string nameArray[], const int size)
{
	using namespace std;
	for (int index = 0; index < size; ++index)
	{
		cout << nameArray[index] << "\n";
	}
}
You should definitely fill the array in a function like in your first code, not in main. Keep main as clean as possible.

As for the sorting part. I mean, if you want to sort numbers smallest to biggest for example that's easy. But how do you want to sort your strings? If I enter 10 names, how do you want to sort them, alphabetically?
If I can't return the array, how do I take input for it outside of main()?

And I did actually get it sorted, I just got confused because I wasn't printing the sorted array at that time. I have it now:

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
#include <iostream>
#include <string>
#include "keep_window_open.h"

int getSizeInput();
void selectionSort(std::string nameArray[], const int size);
void displayArray(const std::string nameArray[], const int size);

int main()
{
	using namespace std;
	cout << "How many names do you wish to enter? ";
	int size = getSizeInput();

	string *nameArray = new string[size];
	for (int count = 0; count < size; ++count)
	{
		cout << "Enter name #" << count + 1 << ": ";
		cin >> nameArray[count];
		cin.ignore(32767, '\n');
	}

	selectionSort(nameArray, size);
	displayArray(nameArray, size);

	keep_window_open();
	return 0;
}

int getSizeInput()
{
	using namespace std;
	int size;
	cin >> size;
	cin.ignore(32767, '\n');
	return size;
}

void selectionSort(std::string nameArray[], const int size)
{
	for (int startIndex = 0; startIndex < size; ++startIndex)
	{
		int smallestIndex = startIndex;

		for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			if (nameArray[currentIndex] < nameArray[smallestIndex])
			{
				smallestIndex = currentIndex;
			}
		}

		std::swap(nameArray[startIndex], nameArray[smallestIndex]);
	}
}

void displayArray(const std::string nameArray[], const int size)
{
	using namespace std;
	cout << "\nHere is your sorted list:\n";
	for (int index = 0; index < size; ++index)
	{
		cout << "Name #" << index + 1 << ": " << nameArray[index] << "\n";
	}
	cout << "\n";
}


I just need to how to take input for the array outside of main() and I'm done.
If I can't return the array, how do I take input for it outside of main()?

Remember I wrote -
arrays are not sent by value like variables, so if it is changed in a function it will be changed everywhere.


Run the program in my second post. It is like your first code without the returning of the array. Consider this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void fillArray(int arr[])
{
	for (int i = 0; i < 3; i++)
	{
		arr[i] = i;
	}
}
int main()
{
	int arr[3];
	fillArray(arr);

	for (int i = 0; i < 3; i++)
	{
		std::cout << arr[i] << " ";
	}
	return 0;
}
So even though function fillArray() doesn't return anything, it still acts as though you filled the array and printed it in main()? Good to know; I'll try that. Thanks.

Edit: When I tried to fill the array in a void function, I got an empty array. The for that function right now is this:
1
2
3
4
5
6
7
8
9
10
11
void getStringArrayInput(const int size)
{
	using namespace std;
	string *nameArray = new string[size];
	for (int count = 0; count < size; ++count)
	{
		cout << "Enter name #" << count + 1 << ": ";
		cin >> nameArray[count];
		cin.ignore(32767, '\n');
	}
}


And main() is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	using namespace std;
	cout << "How many names do you wish to enter? ";
	int size = getSizeInput();

	string *nameArray = new string[size];
	getStringArrayInput(size);

	selectionSort(nameArray, size);
	displayArray(nameArray, size);

	keep_window_open();
	return 0;
}


Edit2: I got it. Passing the array into the function worked.

Edit3: I just remembered to delete the array and set to point to nullptr, so I just typed that in rebuilt it.
Last edited on
Topic archived. No new replies allowed.