> 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.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
> 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
}
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
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 ...
codeaa
Junior Poster in Training
98 posts since Feb 2008
Reputation Points: 13
Solved Threads: 12