Solved

# Integers problem

Posted on 2008-06-10
217 Views
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
Question by:Chrysaor
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 53

Accepted Solution

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
``````
0

LVL 53

Assisted Solution

Infinity08 earned 85 total points
ID: 21754994

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

LVL 86

Expert Comment

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;
``````
0

LVL 45

Assisted Solution

Kent Olsen 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

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month4 days, 2 hours left to enroll