Is it possible to append numbers to the end of variable names during runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbresette183046
    New Member
    • Dec 2012
    • 1

    Is it possible to append numbers to the end of variable names during runtime?

    I am trying to write a program that would convert numbers of base 10, decimal numbers, to binary or hexidecimal numbers, base 2 and base 16. I want the program to run a loop through the various numbers input and store each number converted to the new type in a separate variable with the same basic name but different last letters/digits to differentiate between them and add them to the total.

    Basically, I'm saying that i have the user input a number and letters. Let's say 15, d, b. So they want to convert 15 of decimal type to binary.
    The program would then take the variable used to hold that number, and the other to variables to decide what function to perform on the number.

    Then I will already have a variable initialized for the 3 possible conversions (binaryKey[], decimalKey[], hexideciKey[])

    Then I want it to convert it and store the number at different places in the array to form the final number. Although, there is no way to predict what number the user will input, so there is no way of knowing initially where the converted place-value will need to be placed in the array.

    I was wondering if there was a way to have the program run a loop where as the progression continues, it appends a number to the end of a universal name for the variables and then adds them together in the correct order creating the sequence that means that number.

    In simpler terms:

    Input a number: 15
    Input type of base: d
    Input converted type: b

    Program then continually divides the number by 2, storing the remainder in a new variable

    Such as: for(int i=1, i < (str(number).le n), i++){
    when i = 1, you would get
    int number1;

    when i = 2, you would get
    int number2;

    when i = 3, you would get
    int number3;

    when i = 4, you would get
    int number4;

    and so on. Is there a way to do this??? Please help, I am a highschool student who really enjoys programming and would like some help in applying the languages i've learned.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can't change variable names during runtime. But there are other solutions.

    If you want to study a more advanced topic, you can implement a linked list, then you can add as many items as you want.

    A simpler solution would be to dynamically allocate the array to the size you need.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You can do this:

      Code:
      int Var15;
      int Var25;
      
      int* ptr;      //a pointer to an int
      
      ptr = &Var15;
      
      *ptr = 10;     //goes into Var15
      
      ptr = &Var25;
      
      *ptr = -5;   //goes into Var25
      You effectively change the name of the variable at run time by working through the address of the variable.

      Your functions would have int* arguments and you load the argument with the address of the correct variable when you make the call. It is your responsibility to use the coorect addresses.

      The arrays Rabbit mentions is a common solution to this problem. Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        @weakness, I think the OP's goal is to dynamically create variables with a number appended because they don't know how many they will need until run time.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I would use a vector for this sort of thing, you can not dynamically crate variables but a vector is a type that can dynamically grow.

          With a vector you could fit the whole operation into 1 small loop.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Variable names are only meaningful to the programmer, the compiler and the linker. There are no variable names after the program has been built or while it is running; only addresses.

            Forget about variable names; your program could construct a symbol table during run-time that associates a name string with each entered value.

            Comment

            Working...