473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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).len), 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.
Dec 6 '12 #1
5 2482
Rabbit
12,516 Expert Mod 8TB
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.
Dec 6 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
You can do this:

Expand|Select|Wrap|Line Numbers
  1. int Var15;
  2. int Var25;
  3.  
  4. int* ptr;      //a pointer to an int
  5.  
  6. ptr = &Var15;
  7.  
  8. *ptr = 10;     //goes into Var15
  9.  
  10. ptr = &Var25;
  11.  
  12. *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
Dec 6 '12 #3
Rabbit
12,516 Expert Mod 8TB
@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.
Dec 6 '12 #4
Banfa
9,065 Expert Mod 8TB
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.
Dec 7 '12 #5
donbock
2,426 Expert 2GB
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.
Dec 7 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Harlin Seritt | last post by:
There are certain options for Tkinter widgets that have default values that I don't much care for (borderwidth, font come to mind) and continuously change when I'm building interfaces. With a bit...
4
by: murali pannala via .NET 247 | last post by:
(Type your message here) -------------------------------- From: murali pannala i would want to create an object dynamically. i.e i hold my class name in a variable only.and by passing this...
6
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an...
2
by: ruthboaz | last post by:
Hi, I am creating the following query during run-time. Dim ctl As Control Dim varItem As Variant Dim strSQL As String Dim stDocName As String
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
1
by: Peter Gast | last post by:
Hi, I need as parameter for a control the names of my properties as a string. How can I get the name of the property as a string during runtime Example: Private _MyVar As Double Public...
3
by: Peter Gast | last post by:
Hi, I need as a parameter for a control the names of my properties as a string. How can I get the name of the property as a string during runtime Example: Private _MyVar As Double Public...
2
by: sagarbakliwal | last post by:
hi, I am new to this forum but i have received a lot of help reading through this forum. I am creating a VB.NET application for Patient Record Management. In this there are various combo boxes and...
7
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.