Object trouble

Thread Solved

Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Object trouble

 
0
  #1
Jun 4th, 2009
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:

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class planet{
  7. public:
  8. int moons;
  9. int mass;
  10. }mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;
  11.  
  12. int main()
  13. {
  14. string input;
  15. cout << "enter a planet:";
  16. getline(cin, input);
  17. cout << input << " has " << input.moons << " moons\n";
  18. cout << input << " has a mass of " << input.mass;
  19. return 0;
  20. }
  21.  
  22. /*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.
Last edited by lexusdominus; Jun 4th, 2009 at 12:08 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 62
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Object trouble

 
0
  #2
Jun 4th, 2009
what are you trying to do?

input.moons would mean you are trying to get the moons of a string...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Object trouble

 
0
  #3
Jun 4th, 2009
input.moons?????
Last edited by s_sridhar; Jun 4th, 2009 at 12:29 am.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Re: Object trouble

 
0
  #4
Jun 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Object trouble

 
0
  #5
Jun 4th, 2009
It's so easy: use std::map<std::string,planet>.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Re: Object trouble

 
0
  #6
Jun 4th, 2009
ive been playing around with that for ages & cant get it to work. i cant find any examples of its usage in my context.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Object trouble

 
0
  #7
Jun 4th, 2009
It's so interesting: what's your context? There is a wonderful context: Internet. For example:
http://www.cprogramming.com/tutorial/stl/stlmap.html
and lots of other links to std::map tutorials...
Have you ever heard search Google phrase?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Re: Object trouble

 
0
  #8
Jun 4th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 62
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Object trouble

 
0
  #9
Jun 4th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Re: Object trouble

 
0
  #10
Jun 4th, 2009
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 -

  1. #include <iostream>
  2. #include <string>
  3. #include <map> //include map header
  4.  
  5. using namespace std;
  6.  
  7. class planet{
  8. public:
  9. int moons;
  10. int mass;
  11. }mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;
  12.  
  13. int main()
  14. {
  15. std::map <std::string, planet> planetref; /*declare the map (planetref) its index type (std::string), and what it references*/
  16. planetref["venus"] = venus; /*the map acts like an array
  17. planetref["earth"] = earth;
  18. string input;
  19. cout << "enter a planet:";
  20. getline(cin, input);
  21. cout << input << " has " << planetref[input].moons << " moons\n";
  22. cout << input << " has a mass of " << planetref[input].mass;
  23. return 0;
  24. }
  25.  

Thank god thats over. i wish someone would have just told me. go to google?! kiss my ass.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 517 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC