Arrays

I have a question. I have 5 values that are of different statistics. Is it possible for me to store them all into one string array of size 100?
the values are suntype of string, noOfEarthLikePlanets int, noOfEarthLikeMoons int, avePlasmaDensity float, aveParticularDensity float.


i came up with a logic which is

void locationData::getSunType(string sun)
{
int counter=0;
for(int i=0;i<=counter;i++)
{
data[counter]=sun;
cout<<"The value is"<<data[counter];

}
counter++;
}

However, it can only store a single value. And it keeps overwriting the value that i key in. Is there any other way that i can store all these values into a single array? My professor says it is possible, however i have being reading up the books and found no solution. Can you guys advise me on this?
No, because arrays are a collection of one data type.

What you can do, is create a struct that contains those values, then create an array of that struct.
is it possible to use a class instead of a struct?

correct me if i`m wrong

Yep, they're essentially the same thing.

The difference being access levels; classes are implicitly private whereas structs are implicitly public.
Last edited on
ok so the declaration of my array would be

locationData data[array-size]

using my locationData class as an example.

and how do i store all of the 5 values in them?

should i use back the code that i stated above?

I`m a little mixed up with c++ and java as i come from a java
background

Yeah, that's about right. Here's a quick example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct myStruct
{
   int a, b;
};

myStruct myArray[3];

// You could assign the values a couple of ways
// 1
myArray[0].a = 4;
myArray[0].b = 5;
// And so on

// 2
myStruct localStruct;
localStruct.a = 4;
localStruct.b = 5;

myArray[0] = localStruct;


Hope this helps.

EDIT: I've used a struct again here. If you use classes, you'll need to either make the variables public or put a public method in there to set them.
Last edited on
okay sure. thanks alot
No problem. :-)

In my example I should have really included a couple of different data types in the struct to highlight the point of having an array of them in this scenario.

Hopefully you get the picture, though. :-)
Hey no problem man. I should be thanking you instead, no worries. I will try them out, its part of the learning process anyway =)
i would like to clarify a few things about the code on the struct.

you declared an int a and b in your struct class
both variables are used for the array initialization.

in the following format

myArray[0].a=4;

may i know why what does the .a stand for?

i used the same format as you. However, i used string a and b instead of int.
it complied fine. May i know the logic behind this? could a and b be in any format?
It's just the dot operator.

It means it's accessing the variable a, of the struct.

It'll work fine for strings and, in fact, any data type in the struct (provided it's not another nested struct, in which case you'd need another dot operator).

[struct/class][dot][member of that struct/class]

If you had the struct:
1
2
3
4
5
struct MyStruct
{
   string name;
   int age;
};

Then you'd access them like this:
1
2
3
4
MyStruct MyArray[3];

MyArray[0].name = "John Doe";
MyArray[0].age = 30;
Last edited on
i see. So if i would like to call out the values from the array.
Correct me if i`m wrong.

it would be like this.

for(int i=0;i<=counter;i++)
{

cout<<"The values are"<<myArray[counter];


}
You'd have to specify which member you're accessing.

So if it was name and age as in my example above, it would be:
1
2
3
4
5
for(int i=0; i<=counter; i++)
{
   cout << "Name is " << myArray[i].name << endl;
   cout << "Age is " << myArray[i].age << endl;
}


EDIT: You could overload the << operator for that struct, but that's a whole different ball game.
Last edited on
I see. Thanks alot for your help! =)
Now i can finish my assignment.
Topic archived. No new replies allowed.