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?
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.
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.
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 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).