how to add a variable in an other variables name?
-
Hello,
I am trying to make a program, where i have 10 names, and 10 results, but i have 50 things to choose from.
You have to select a checkbox to add that thing to a name and a result.
Now i am trying to find out how ta add a number to a variables name, so i dont have to make a whole lot of code.
this is my example:{ qDebug ()<<"start"; counter=1; if (ui->checkBox->isChecked()){ name+(counter) = "first name"; result+(counter) = "first result"; counter++; } if (ui->checkBox_2->isChecked()){ name+(counter) = "second name"; result+(counter) = "second result"; counter++; } qDebug()<<"first name = "<<name1; qDebug()<<"first result = "<<result1; qDebug()<<"second name = "<<name2; qDebug()<<"second result = "<<result2;
So if you click checkbox 1, the string of the first name has to go to name1, same with result.
if i click only checkbox 2, then the string of the second name has to go to name1.
if i click them both, then the string of the first name has to go to name1, and of the second name has to go to name2.
Is there an easy way to do this?(i hope you understand what i mean, it is a bit difficult to explain for me.
-
@SGaist said in how to add a variable in an other variables name?:
Hi,
You can't do that with C++. But from the looks of it, a QMap would likely do the job.
Thanks for your answer, I will do it the "hard" way, just code it one for one, might be easier to handle the results as qmap I think, because I have no experience with that.
-
Fill all results of checked check boxes into a list, and then take the first n entries from that list (up to the number you need, or the size of the list)
EDIT: If you put the checkboxes into a container, you just have to loop over it