I'm teaching myself C++ through googling (my class has no textbook, if you can believe it) so I apologize in advance for my (probably) incorrect terminology.

I want to name individual incarnations of a struct by numeric information supplied by the user. Here is a snippet:

int main (void)
{

    int num;

    struct Serialnumber
	{
		string Revision, Date, Department; 
        };

cout << "Please enter the number of the part you wish to find.\n";

cin >> num;

}

So, if the user inputs 1234 for num, can I make a structure where the name equals the value of num, i.e. Serialnumber 1234?

In the above format, the complier tells me that num is already declared (as the int in the beginning). I'm using Dev C++ 4.9.9.2 if that matters.

I would be most appreciative if anyone could point me towards the appropriate direction, such as a command type. I'm finding it difficult to look up an idea when I don't already know the right name for it.

Thanks for your help. This community's archives have been a godsend for me through the past several months.

> So, if the user inputs 1234 for num, can I make a structure
> where the name equals the value of num, i.e. Serialnumber 1234?

1234 is not a valid C++ identifier. but ven for a valid identifier ( like sn_1234 ),

the short answer to your question is no.
names (identifiers) are compile-time entities. they are used by the compiler at compile-time. identifiers need to be specified at the time you write your code.
modifiable values are run-time entities. eg. a value supplied by the user are available only when the program (that you had compiled earlier) executes.

a longer answer would be: it is possible to get the effect you are looking for by writing a source code generator and dynamically compiling and executing the generated source code. (an explanation of how to do this should go here. but, since you are right now just beginning to learn C++, do not even consider this to be a possibility.)

why is it that you are trying to do this (want to name individual incarnations of a struct by numeric information supplied by the user)? if you posted
a. what is the program that you are trying to write supposed to do? and
b. where are you encountering a problem in writing it?
there would be many here who could help. and you might discover that the solution to the real problem is surprisingly simple.

a. what is the program that you are trying to write supposed to do? and
b. where are you encountering a problem in writing it?
there would be many here who could help. and you might discover that the solution to the real problem is surprisingly simple.

Thanks so much for your reply. :)

a. The assignment is to use multiple commands (functions, arrays, classes, and be menu-driven) and the program is intended to fill a need at the student's place of employment. Since I work with machinery, my program is supposed to input and display CAD drawings by serial number, and include their revision number (int), date of revision (int), and the department who requested the revision (char, because we have one-letter codes for this).

b. My problem is that the serial numbers are not necessarily created consecutively (due to the naming convention of number places meaning different things...it's a mechanical engineering thing. That's another rant.). Counting upwards in order might result in 1257, 1489, 1490, and 1850, with no numbers in between those. Without using an array (all my data types are not the same) or a loop (which won't work because of the non-consecutive numbers) my next thought was to make the user's inputted serial number become the name of a struct that holds the other pieces of information.

I have the input for the other characteristics done in functions, like below, but a collective data type for numbers that could be anything from 1000 to 9999 with possible empty spaces throughout is tripping me up.

void InputRev (void) //function for inputting revision number
{

int serialnum;
int revisionnum;

	cout << "Please enter the serial number of the part whose revision number you wish to input. Serial numbers may be between 1000 and 9999.\n";

	cin >> serialnum;

	if (serialnum < 1000 || serialnum > 9999)
	{
		cout << "That is not a valid selection. Please choose again.\n\n";
		cin >> serialnum;
	}


	Information (serialnum); //Here is what I tried to do that isn't possible

		   do
		   {
			cout << "Please enter the revision number for part " << serialnum << ".\n";
			cin >> revisionnum;
				   
};

> Without using an array (all my data types are not the same)
there are two ways to solve this. either use a struct which collects information about one cad drawing in one place or use multiple arrays; one array for each part of the information. since i'm not sure that you are familiar with struct or class , let us look at using many arrays.

enum { MAX_DRAWINGS = 500 }; // maximum possible
int num_drawings = 0 ; // actual number
int serial_number[ MAX_DRAWINGS ] ;
int revision_number[ MAX_DRAWINGS ] ;
int date_of_revision[ MAX_DRAWINGS ] ;
char department[ MAX_DRAWINGS ] ;

for input of information, we write a loop of this kind:

int i = num_drawings ;
for(   ; i < MAX_DRAWINGS ; ++i )
{
   // get serial number, put it in serial_number[i]
   // get revisionnumber, put it in revision_number[i]
   // get date of revision, put it in date_of_revision[i]
   // get department, put it in department[i]
   // break out of loop if end of user input
}
num_drawings = i ;

at the end of this serial_number[15] will contain a drawings serial number (say 1382),
revision_number[15] will be its revision number,
department[15] will be its department etc.

to get information about a drawing with a specific serial number,
a. get the position by searching in the array serial_number
b. get the other information from the same position in the other arrays

for( int pos = 0 ; pos < num_drawings ; ++pos )
  {
     if(  serial_number[pos] == number_we_are_looking_for )
     {
        // revision_number[pos] is its revision number
        // date_of_revision[pos] is its date of revision
        // department[pos] is its department
        // we are done
      }
  }

That makes so much more sense than my idea. I should have been thinking in terms of clustering all data types under a common number (the array location) rather than attempting to use the serial number as the defining factor.

Thank you!

Adapting from your original post, why not include the serial number in your struct, calling it Drawing, and have an array of those. Something like this maybe:

struct Drawing
{
    int serial_number;
    int revision;
    int revision_date;
    char revision_department;
};

Drawing drawings[NUM_DRAWINGS];

drawings[0].serial_number ...
drawings[0].revision ...
drawings[0].revision_date ..
drawings[0].revision_department ...
This article has been dead for over six months. Start a new discussion instead.