Variable object names

This is a discussion on Variable object names within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a bit of code that can read information from a file, and based off this information, ...

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

    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:

    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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > 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.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    And How do you plan to access these objects?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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, 12:26 PM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 05: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 Cprogramming.com
    Replies: 1
    Last Post: 06-29-2002, 01:30 PM
  5. variable names
    By trekker in forum C Programming
    Replies: 3
    Last Post: 03-16-2002, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21