Solved

Integers problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

26 Experts available now in Live!

Get 1:1 Help Now