-
April 5th, 2009, 06:24 AM
#1
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?
-
April 5th, 2009, 06:43 AM
#2
Re: How to ctreate an array of objects that contain character strings
Strings: string items[5];
General: myclass items[5];
To retrieve the item "n" from the string solution, just items[n].
To retrieve the item "n" from the "myclass" solution, overload the operator [ ], and then just items[n];
Edit:
Maybe the problem is you don't know how to create the class. Example:
Code:
class MyClass{
public:
string& operator[](int n){ return item[n]; }
private:
string item[5];
}
Last edited by Tronfi; April 5th, 2009 at 06:46 AM.
-
April 5th, 2009, 10:33 AM
#3
Re: How to ctreate an array of objects that contain character strings
Originally Posted by Tronfi
Edit:
Maybe the problem is you don't know how to create the class.
Why create a class? The array is all that's needed.
Regards,
Paul McKenzie
-
April 5th, 2009, 10:57 AM
#4
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).
Last edited by Lindley; April 5th, 2009 at 02:55 PM.
-
April 5th, 2009, 11:52 AM
#5
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.
-
April 5th, 2009, 01:51 PM
#6
Re: How to ctreate an array of objects that contain character strings
Fully dynamic
Code:
std::vector<std::string> myarray;
-
April 5th, 2009, 06:43 AM
#7
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
Last edited by richard_tominez; April 5th, 2009 at 06:52 AM.
-
April 9th, 2009, 02:35 AM
#8
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.
-
April 9th, 2009, 01:59 PM
#9
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.
Where is mistake in this code? because it is not working yet..
#include <iostream>
#include <string>
using namespace std;
int main(){
char s[5];
char array[5][80];
cout<<"Enter 5 words followed each by <enter>"<<endl;
for(int i=0; i<5; i++)
{
cin.getline(s[i],20);
}
for(int j=0; j<5; j++)
{
cout << s[j]<<endl;
}
return 0;
}
many thanks.
Paul.
Last edited by itguy310; April 9th, 2009 at 02:12 PM.
-
April 5th, 2009, 02:14 PM
#10
Re: How to ctreate an array of objects that contain character strings
Originally Posted by STLDude
Fully dynamic
Except that leonida explicitly wants an array of 5 string objects, so if we are to use a container instead of a C-style array, it should be:
Code:
std::tr1::array<std::string, 5> names;
-
April 8th, 2009, 01:18 AM
#11
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?
I appreciate your help.
-
April 8th, 2009, 03:55 AM
#12
Re: How to ctreate an array of objects that contain character strings
Originally Posted by leonida
Thanks for your replies. Tronfi, you got the point, I'm looking strictly for a solution with class objects.
I don't really understand what you really want. Do you want an array of strings? If you do, what was shown to you by others is what you are asking for.
If you want an array of an object, and each object contains a string, that is a different story.
So which one do you want? If it is an object with a string, then here it is:
Code:
class SomeObject
{
private:
std::string m_myString;
public:
// other members
};
int main()
{
SomeObject arrayOfObject[5];
}
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.
This can easily be done with an array of std::stringUnless you mean use the std::string class, this doesn't make sense. You need to explain exactly what your requirements are.
Regards,
Paul McKenzie
-
April 8th, 2009, 05:13 AM
#13
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.
-
April 8th, 2009, 05:25 AM
#14
Re: How to ctreate an array of objects that contain character strings
Originally Posted by leonida
but my confusion is how to create an array of objects, a data member should be a pointer to null-terminated string
As demonstrated, use a std::vector<std::string>. You would access it just as you would a "normal" array.
If due to assignment requirements you are not allowed to do so, then...
Originally Posted by leonida
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.
Let's see your current code. Chances are, you should be creating your own string class.
-
April 8th, 2009, 10:47 PM
#15
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.
#include <iostream>
using namespace std;
#include <string>
int main(){
char s[5];
cout<<"Enter 5 words followed each by <enter>"<<endl;
for(int i=0; i<5; i++)
{
cin.getline(s[i],20);
}
for(int j=0; j<5; j++)
{
cout << s[j]<<endl;
}
return 0;
}
error: cannot convert parameter 1 from 'char' to 'char *'
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|