Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Integers problem

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
Chrysaor
Asked:
Chrysaor
  • 2
3 Solutions
 
Infinity08Commented:
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
 
Infinity08Commented:
A bit more info on arrays :

        http://www.cplusplus.com/doc/tutorial/arrays.html
0
 
jkrCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now