Hello! google has turned me up at this forum so many times i think it would be stupid to ignore the hints & not get registered! Im having some problems with objects. Here is my code:

#include <iostream>
#include <string>

using namespace std;

class planet{
      public:
int moons;
int mass;
}mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;

int main()
{
string input;
cout << "enter a planet:";
getline(cin, input);
cout << input << " has " << input.moons << " moons\n"; 
cout << input << " has a mass of " << input.mass;
return 0;
}

/*i would like to know the quickest way to acheive this effect - the closest my knowledge allows involves nesting 8 if statements for each planet name */

Ive been pointed a couple of times in the direction of maps, but ive no idea how id have to implement them in this scenario. Any help would be much appreciated! Ive run into this before & its been annoying me for the past 5 hours. i really cant understand maps.

what are you trying to do?

input.moons would mean you are trying to get the moons of a string...

input.moons?????

Im trying to reference the object name with the string, for example input.moons becomes earth.moons, assuming "earth" is in the string. I know this is not legal C++ but id like to acheive this effect.

i didnt know you could create arrays of objects, ill have a quick gander & see if i can come up with something

It's so easy: use std::map<std::string,planet>.

ive been playing around with that for ages & cant get it to work. i cant find any examples of its usage in my context.

My context - i havent seen it used in the way i would like to use it. i didnt come here to teach english.

ive been looking on the internet for a while now like i said, and i cant really grasp the way in which map can help me. and, not being funny or anything, but i came here for a nudge in the right direction, not for a link to google..

well a map is going to associate a key with a value, so if you set your keys to your planet names and the values to your objects (planets), you can retrieve them based on the name (key).

Ok! i got it. thank god that was really frustrating. i still dont fully understand it but now i have working code ill be able to break it apart in more detail. for anyone else who gets stuck with this , here is the updated code -

#include <iostream>
#include <string>
#include <map>  //include map header

using namespace std;

class planet{
      public:
int moons;
int mass;
}mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;

int main()
{
std::map <std::string, planet> planetref; /*declare the map (planetref) its index type (std::string), and what it references*/
planetref["venus"] = venus;               /*the map acts like an array 
planetref["earth"] = earth;
string input;
cout << "enter a planet:";
getline(cin, input);
cout << input << " has " << planetref[input].moons << " moons\n"; 
cout << input << " has a mass of " << planetref[input].mass;
return 0;
}

Thank god thats over. i wish someone would have just told me. go to google?! kiss my ass.

Well, is it an unique case of std::map using?
It looks like an ordinar, absolutely standard case which answers your requirements...
;)

I didnt say it was unique, i just couldnt find any examples of it being used to for that purpose so i wasnt actually sure how to implement it. its a waste of time playing guessing games with compilers. anyway it doesnt matter now. its done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.