Checking if a variable exists:
Hello everyone- this must be a simple problem, but I can't find a solution everywhere.
I have a set of variables (created via struct):
1 2 3
|
struct problemSheet {
string name;
}
|
where:
1 2 3 4 5 6 7
|
problemSheet sheet1;
sheet1.name="Problem Sheet 1";
problemSheet sheet2;
sheet2.name="Problem Sheet 2";
etc...
|
now I want to output the names of all the variables ie:
1 2
|
cout << sheet1.name << endl;
cout << sheet2.name << endl;
|
but now I want to do this automatically ie- output all the sheets stored in the memory by going through sheet1, sheet2, sheet3...
I tried to do this via checking the existence using stringstream, but this checks the existence of the string stream, rather than the variable I want:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
outputKounter=0;
while (1){
outputString.str("");
outputString << "sheet" << outputKounter;
cout << outputString.str() << endl;
if (outputString.str()){
cout << "EXISTS!" << endl;
}
else{
break;
}
outputKounter++;
}
|
Any ideas? I've been at this for a few days, and there must be a simple answer...
Don't do this. AFAIK this is reflection and it's hard to implement in C++.
Use an array of structs instead.
Topic archived. No new replies allowed.