# Can I have an "Array of Strings"?

This is a discussion on Can I have an "Array of Strings"? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I don't think I will be able to find the correct words to explain what I want but ...

1. ## Can I have an "Array of Strings"?

Hello everyone,

I don't think I will be able to find the correct words to explain what I want but I will just give it a try....

I have to make a program which takes 5 names as input and sorts them in alphabetical order. I have seen the program which sorts numbers in ascending or descending order by comparing one by one and then swapping them(I think it is called quicksort). Now I want an array of strings to store the names. I tried to do something with classes but then got confused

I am a beginner so don't use heavy concepts please

Thank you..

2. Yes you can.

Can you please post your code to read one name, so that I can determine what level of multiple names functionality would be suitable.

--
Mats

3. Here is what I tried to dohope it makes some sense)
Code:
#include<iostream>
using namespace std;
void sort();
void display();
class name
{
private:
char nm[20];
public:
name()
{
cout<<"Enter the name";
cin>>nm;
}
void show_name()
{
return nm;
}
}

int main()
{
name nm1,nm2,nm3,nm4,nm5;
sort();
display();
cin.get();
return 0;
}

4. Ok, so if you are using character arrays as strings (rather than C++ std::string), then you will need to use a 2D array, something like this:
Code:
char name[5][20];
--
Mats

5. Thanks! I am going to try it right now. I will come back if I need some more help....

6. string is an STL collection of characters, you should use an STL container such as a vector or list to store them. using C style arrays when you have these vectors makes use of the STL.

Code:
std::vector<std::string> strCol;
strCol.push_back("SomeString);
/...etc etc
This dynamically resizes the vector so you don't have to when doing something suchj as sort or remove etc. Most of the vector interface is supplied in the class.

http://www.cppreference.com/cppvector/index.html

7. indigo: I choose to NOT suggest that, because I believe that the poster is currently working on character arrays to learn those - whilst using vectors certainly is a good thing, I don't agree with suggesting a completely different system for almost the entire functionality in the code.

--
Mats

8. I don't know, I just find if he's a beginner it would be easier to understand the abstracted string and vector class rather than the underlying implementation which is what he's doing. To create a string he'd have to jump through hoops to resize it, then dynamically allocate a new vector for the alpha sorting. I think it's more trouble than it's worth. He can learn about the C origins later, but I think it would hurt to try to understand that first. Mainly because it introduces the idea of pointers too early for him to make use of properly.

9. Sure.

The other question that is relevant is if this is a school assignment or self-education. In the former case, the class direction would also be relevant.

--
Mats

10. If the OP doesn't have to, he shouldn't. Otherwise I'd tell him to punch his teacher.

11. ## I Wrote This:

I am trying something similar to quicksort.
I wrote this but it is giving me some errors:
Code:
#include<iostream>
#include<algorithm>
void sort();
void display();
char names[5][20];
using namespace std;
int main()
{
char temp[20];
int x=0,y=0;
for(x=0;x<=4;x++)
{
cout<<endl<<"Enter the "<<x<<"th name";
cin>>temp;   //I used this temporary string for reason I can't explain in words!
for(y=0;temp[y]!='\0';y++)
{
names[x][y]=temp[y];
}
names[x][y]='\0';
}          //I have tested the code upto this point.I could make five inputs and then print them one by one.
sort();
display();
cin.get();
return 0;
}

void sort()
{
int x,y,var,num1,num2;
for(x=0;x<4;x++)
{
for(var=x+1;var<=4;var++)
{
for(y=0;((names[x][y]!='\0')&&(names[var][y]!='\0'));y++)
{

num1=(int)names[x][y];   //This is to get the ASCII value

num2=(int)names[var][y];//This is to get the ASCII value

if(num1>num2)
{

swap(names[x],names[var]);
}

}
}
}
}

void display()
{
for(int x=0;x<=4;x++)
{
cout<<endl<<names[x];
}
}
I know I have not made the code readable.....sorry for that
Code:
The error messeges are:
C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h In function void std::swap(_Tp&, _Tp&) [with _Tp = char[20]]':
43 C:\Dev-Cpp\alpha.cpp   instantiated from here
130 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h invalid initializer
131 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays
132 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays`

What do you think is the problem? (If you can read that bad coding)
Thanks!

12. This is self-education but I don't know anything about vectors (I have studied vectors in physics--magnitude and direction, will that much do?)

13. You might try std::string then. For example, two std::strings can be compared with the comparison operator, but you'd need to use strcmp for C-style strings. std::strings can be assigned, whereas arrays need to be copied each element individually.

In addition std::string automatically avoids buffer overruns which your current code is in danger of (what if I typed more than 19 characters?)

In std::vector vector simply means a sequence. It is basically an array, but it is smarter than a plain array in the same way a std::string is smarter than a C-style string (e.g knows its size, can resize itself automatically, automatically manages memory etc).

14. also the algorithms library you included is made to handle all sorts of Generalized STL Containers such as vectors and lists.

15. >> also the algorithms library you included is made to handle all sorts of Generalized STL Containers such as vectors and lists.

The algorithms library is generic and works on C style arrays just as well as C++ strings, vectors, lists, etc.

That said, I, too would suggest learning C++ strings and vectors rather than C style arrays. There is very little reason to use the C style versions in C++ programs. The string class is rather easy to use.

Page 1 of 2 12 Last
Popular pages Recent additions