Understanding strings and arrays

Hello everybody.

I'm trying to store a list of names but the user defines the size of the list and then they input the actual names.

I was trying this code:

1
2
3
4
5
6
  int size;
  string dogsnames[];
for (int i=0; int<=size; i++)
  { 
      cin >> dogsname[i];
  }


But obviously it's not working, and I'm not sure how to make an array of strings.
I was thinking of taking an approach like this:
1
2
3
4
5
6
7
void dogs(int * names, int 0, int size)
{
	for(int i=0; i<=size; i++)
	{
		cin >> names[i];
	}
}


But I'm not entirely sure how to use it.
Thank you in advanced!

edit: The actual program has to output the names alphabetically, but the sorting I've pretty much got down.
Last edited on
closed account (48T7M4Gy)
To declare an array dynamically, at run-time you need to use the keyword new, later followed when the array is no longer required by delete.

Checkout the tutorials here on this website for details.

Or try this one:-
http://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/
Last edited on
you need to use the keyword nee,


Typo error: Better use new. nee is a type error.
Thank you very much ! I made it like this:

1
2
3
4
int size; 
string * names;

names = new string[size];

That has the array of strings made. I also have the user input coded.

Now, I know I edited my post saying I had the sorting down, but I have it down for int type arrays. Those are way easier! I have used a code for this before:
1
2
3
4
5
6
7
8
void sort(int * numbers)
{
	for(int i=start; i<=end; i++)
	{
		int first = firstNum(nums,i,end);
		swap(numbers[i], numbers[firstNum]);
	}
}

Similar to this one, where firstNum was a function to find which number would go first, or the smaller one and does not return anything because it's just moving numbers in an array around.

How could I go about to sorting a string array alphabetically?

Thank you in advanced!
closed account (48T7M4Gy)
http://www.cplusplus.com/articles/NhA0RXSz/
Topic archived. No new replies allowed.