Let's say I have the integers A1,A2,A3 and 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?

Thanks a lot.

Let's say I have the integers A1,A2,A3 and 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?

Thanks a lot.

I'm assuming you are using MS Visual C++?

I think you should create an array called A and refer to the elements as A[1], A[2], A[3], etc, rather than A1, A2, A3, etc. If there are relatively few integers, you could hard code A1, A2, A3, but it will be a pain to hardcode variable names with if or switch statements if there are a lot of possible values of x, so use an array. You could set the text to display like this, for a value of x:

String textToDisplay = A[x].ToString();

Say your label is called "myLabel". To set the text, you could do this:

myLabel.Text = textToDisplay;

I'll give it a try:

#include <iostream>
using namespace std;

int A[1000]     //for global scope
// you should still assign values to it in here (in a for loop for example)

void displayvar(int x){
cout<<A[x];
}

int main(){
int x;
cout<<"Value of X: ";
cin>>x;
displayvar(x);
system("pause");
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.