-
Variable object names
I'm trying to create a bit of code that can read information from a file, and based off this information, create a number of objects. However... I want every object created to be called U001 where 001 increments each time an object is created.
How do I go about this?
for reference, here's the code I already have:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Unit
{
public:
int HP;
int Xcord;
int Ycord;
char ident[8];
char house[8];
char weap1[8];
char weap2[8];
char dispnam[32];
};
int main ()
{
cout << "Available sides:\n* ASIDE -- Side #A\n* BSIDE -- Side #B\n* CSIDE -- Side #C\n\n";
// Defining Variables used, creating an fstream object called Fileop
char LineT[256];
int LineN; LineN = 0;
fstream Fileop;
// Opening file Units.dat for input & Output
Fileop.open ("Units.dat",ios::in);
// Read and display file, 1 line at a time
while (!Fileop.eof ( ) ) {
Fileop.getline (LineT,256);
cout << LineT; cout << "\n";
if (strncmp (LineT,"-",1) == 0) {
Unit U001;
U001.HP = 400;
};
LineN = LineN + 1;
};
// Close the opened file
Fileop.close();
cin.get();
return 0;
}
All help is appreciated! :)
-
> object created to be called U001 where 001 increments each time an object is created.
You mean like an array, or a vector (since you want something which expands)
U[1]
U[2]
etc etc
> while (!Fileop.eof ( ) )
See the FAQ for why using feof here is bad.
-
And How do you plan to access these objects?
-
You could always use a std::map<std::string, YourClass> where you use, for example:
classmap["U001"] = YourClass(...);
-
Well, you could
1. Implement a scripting language interpreter in C++ or integrate another language, like Python.
2. Implement underlying C++ code as a library or extension to the scripting language.
3. Finally, use dynamically created variable names in the scripting language, calling the underlying library to perform low-level tasks.