| | |
Object trouble
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
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:
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.
C++ Syntax (Toggle Plain Text)
#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.
Last edited by lexusdominus; Jun 4th, 2009 at 12:08 am.
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
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
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 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?
http://www.cprogramming.com/tutorial/stl/stlmap.html
and lots of other links to std::map tutorials...
Have you ever heard search Google phrase?
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
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..
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).
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
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 -
Thank god thats over. i wish someone would have just told me. go to google?! kiss my ass.
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
- Web Spidering (Python)
- Saving output of script to text file (Python)
- adding a bunch of List object to a set, and a bigger problem (Java)
- Casting object to double. (Java)
- Object help (Java)
- class based on Tk() . example (Python)
- Problem Passing a Class Object to a Queue (C++)
- trouble with "click me"/Canada.exe (Viruses, Spyware and other Nasties)
- Printer Object Question (Visual Basic 4 / 5 / 6)
- rundll32 trouble (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: need some guidance
- Next Thread: How to input a telephone number and use function strtok to extract?
Views: 517 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c/c++ calculator char class classes code command compile compiler console constructor conversion convert count data delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee image input int lazy library linker list loop looping math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock






