Solved

Integers problem

Posted on 2008-06-10
4
212 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now