-1
\$\begingroup\$

I didnt know how to title this...

Background: I am currently trying to develop a windows command console game with C++ for a final project. The game is text based. I have only been programming C++ for a semester. My game is basically a racing simulation game where you can buy and equip different cars.

I have created a bunch of different cars with stats similar to the Forza 7 racing game. The stats are as followed: car model and year, car ID, top speed (float), handling (float), acceleration (float), braking (float), as well as some hidden attributes like ride height(enum) and others.

Goal: I am basically trying to find a way to store this data in large amounts while being able to edit and access this data very easily. I want to be able to print these out in large amounts (possibly using a loop to print based on car IDs) and be able to set the equipped cars stats equal to an int that is always being changed to the equipped car's stats.

I was thinking placing these statistics inside of a structure and then initializing each car and its attributes individually, but this would be a very long process. Example of what I think I should do:

struct carData {
   string Year;
   string Manufacturer;
   string Model;
   int iCarID;
   double iCost;
   float fSpeed;
   float fHandling;
   float fAccel;
   float fBraking;
   float fMidRange;
};
//Every car, the number represents the car id

carData C01;
C01.iCarID = 01;
C01.Year = "2001";
C01.Manufacturer = "Volkswagen";
C01.Model = "Golf";
C01.fSpeed = 9.9;

//Would be continued for all stats for all cars

Would it be possible to then print this data (cars name, number and stats) using a loop rather than printing it individually when needed? I want to be able to change car's stats without having to redo entire sections of code and I want to keep my code as efficiently as possible.

Any resources or ideas would be greatly appreciated. If you find links to similar questions, please let me know. I had no idea what to search to find a question like mine... Thanks.

\$\endgroup\$
1
\$\begingroup\$

Store the individual instances of your data in a std::vector (std::vector<carData> allTheCars).

You can then use resize() to pre-allocate a bunch of entries in the array, and refer to individual entries with operator[] (for example, allTheCars[0] or allTheCars[21]).

You can also use reserve() to reserve storage without actually adding elements, and then use push_back() or emplace_back() to add them on at a time (particularly if you are reading the data from a file, for example).

Regular C++ for loops can be used to iterate over the data in a loop (for (auto i = 0; i < allTheCars.size(); ++i) { ... }). You can also use range for loops, as well.

\$\endgroup\$
  • \$\begingroup\$ Would I create a new vector array for each element of my cars. Like would I create a 'vector' for the car id, another for the car's model, and more for the speed and such? \$\endgroup\$ – DarkMilk Feb 15 '18 at 0:27
  • \$\begingroup\$ While that is an appropriate technique sometimes, it's probably not in your case. Put all the car data into a structure, put those structures into a single array. \$\endgroup\$ – Josh Feb 15 '18 at 3:58
  • \$\begingroup\$ Sadly, I checked this too late and therefore I got your message too late. What I set up was a comma delimited file that had all the data imported into separate vectors. It works fine, it just isnt as efficient as what you explained above. Thanks for all your help. You really helped solve my problem that I literally spend days thinking about different ways I could do it. Thanks again. \$\endgroup\$ – DarkMilk Feb 16 '18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.