Solved

Integers problem

Posted on 2008-06-10
4
214 Views
Last Modified: 2010-05-19
Let's say I have the integers A1,A2,A3 .....Ax, and the integer x.

I want when the value of x is 2 (for example) , the caption of a label to return A2's value. If x = 3, to return A3 value and so on. How do I do that? (Without having to write manually each time if x= 2, label->caption = A2 etc, but automatically to return Ax value)

Thanks a lot.
0
Comment
Question by:Chrysaor
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 85 total points
ID: 21754984
Place the integers in an array :
int a[10] = { 0 };          // <--- an array of 10 integers
int x = 5;
int value = a[x - 1];       // <--- the x-th value in the array

Open in new window

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 85 total points
ID: 21754994
A bit more info on arrays :

        http://www.cplusplus.com/doc/tutorial/arrays.html
0
 
LVL 86

Expert Comment

by:jkr
ID: 21755005
See the reference to the AnsiString class at http://thunder.prohosting.com/~cbdn/pg_013.htm - you can do that like
AnsiString str;
int x = 2;
 
str = "A" + IntToStr(x);
 
label->caption = str;

Open in new window

0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 40 total points
ID: 21755006
Hi Chrysaor,

You can either build the lengthy structure that you're trying to avoid, or you can use an array to contain the addres of the integers.

int A0, A1, A2, A3, A4, ..., A20;
int *A[21] = {&A0, &A1, &A2, &A3, ... &A20};

  Label->Caption = Format ("%d", ARRAYOFCONST((*A[x])));


And of course, you can also skip the step of using scalar integers and putting their addresses into an array by just using an array of integers.


Good Luck,
Kent
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question