Solved

Integers problem

Posted on 2008-06-10
4
215 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:Kent Olsen
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 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