Thread: Variable object names

  1. #1
    Registered User Rider's Avatar
    Join Date
    Nov 2005

    Question 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:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    class Unit
      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 ("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        
      return 0;
    All help is appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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)

    etc etc

    > while (!Fileop.eof ( ) )
    See the FAQ for why using feof here is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    And How do you plan to access these objects?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2003
    You could always use a std::map<std::string, YourClass> where you use, for example:

    classmap["U001"] = YourClass(...);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Mar 2006
    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.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random variable names
    By NeMewSys in forum C Programming
    Replies: 7
    Last Post: 06-12-2008, 11:26 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. Variable Names based on Variable Values
    By mrpickle in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2003, 10:33 AM
  4. Valid variable and function names
    By sean in forum A Brief History of
    Replies: 1
    Last Post: 06-29-2002, 12:30 PM
  5. variable names
    By trekker in forum C Programming
    Replies: 3
    Last Post: 03-16-2002, 03:37 AM