If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to ctreate an array of objects that contain character strings

I've just strated learning object oriented c++ and I'm having a lot of confusion with it. Could anyone tell me how to create an array of objects that contain character strings. I want to input the names of 5 items. How do I create an array that contains these 5 words, and then how do I retrieve those words from the array?

Re: How to ctreate an array of objects that contain character strings

Code:

char array[5][80];
/*Declares an array that has 5 elements.
Each of the element has 80 elements.
The first dimension represents the 5 words and the second dimension represents the maximum length of each words.*/
for(int i=0; i<5; i++)
{
std::cout << array[i] << "\t";
}//Prints the 5 words.

You can also use the string class. To use it, include string to your cpp file

Re: How to ctreate an array of objects that contain character strings

The solution that most closely matches the OP's request is simply

Code:

std::string myarray[5];

since a std::string is an "object that contains a character array".

The example given by richard_tominez is simply an array of arrays, not an array of objects; and it's less safe to work with (although perhaps marginally faster in some cases...not enough to worry about usually).

Re: How to ctreate an array of objects that contain character strings

Originally Posted by Paul McKenzie

Why create a class? The array is all that's needed.

Regards,

Paul McKenzie

I know it's not neccesary to create a class, but I supoussed (maybe wrongly) that it was what he wanted. I thought he knew how to use string (I said how to make an array of strings in the beginning of my reply), but he said he was new in "object oriented c++", so I give him a example with a home-made class.

Re: How to ctreate an array of objects that contain character strings

Thanks for your replies. Tronfi, you got the point, I'm looking strictly for a solution with class objects. I know how to create arrays but I don't know how to do it within a class.

Please can someone show me a code where you let user enter 5 words, which are stored in array and then you access those words in array through iteration loop and display. It has to use class. Is it possible to do such thing?

Re: How to ctreate an array of objects that contain character strings

Paul, when I say class, I don't mean specific one, just as many classes as necessary, but my confusion is how to create an array of objects, a data member should be a pointer to null-terminated string. I don't understand how to input string objects into an array., I do know how to declare an object whose data member is a pointer to string, but I really don't know how to implement that into arrays and then retrieve data from an array.

Could you show me just a minimum piece of code that declares an array of objects, lets say, how would you imput 3 words: for example "cats", "dogs", "ducks" and then display the word "cats" in cout statement. I just want to see how you index those words.

Re: How to ctreate an array of objects that contain character strings

I figured out my confusion is not coming from classes, I'm sorry if my previous posts were confusing, I was confused myself. I realized I don't know how to create the right input and output, can you tell me what is wrong in my code? This is something simple that I got stuck with.

Re: How to ctreate an array of objects that contain character strings

Originally Posted by richard_tominez

Code:

char array[5][80];
/*Declares an array that has 5 elements.
Each of the element has 80 elements.
The first dimension represents the 5 words and the second dimension represents the maximum length of each words.*/
for(int i=0; i<5; i++)
{
std::cout << array[i] << "\t";
}//Prints the 5 words.

You can also use the string class. To use it, include string to your cpp file

This solved my mistake, I forgot to enter the length of words [80]. Thank you everybody for trying to help! I'm new to this forum and I'm glad there are people like you who are willing to spread the knowledge.

Re: How to ctreate an array of objects that contain character strings

Hello leonida,

i got what you trying to ask and you coded very well....actually i am also new to c++ and working on it.

You want input from the user of five words then sort them into ascending order. i tried your code but i am getting error can you tell me what was the problem in array string and can you please write and justify the complete code so that i can also understand it more and can code it by myself.