C++ help please!

Rapier21

Member
Aug 5, 2001
112
0
0
What I'm trying to do is to initialize a bunch of variables of type Question with incremental file names such as q1, q2, q3, q4, etc. I want the number of variables to make be a previously defined int NumQuestions. Here's what I have so far:

for(int i = 1; i<=NumQuestions; i++)
{
Question q char i;
}

Of course this doesn't work, but somehow I need the value of i to be part of the variable name. Any thoughts?

~Rapier21
 

Cat

Golden Member
Oct 10, 1999
1,059
0
0
I assume you're familar with string, and iostream.

#include <string> // redundant, I think
#include <strstream>

strstream tmp;
for( int i = 0; i < numQuestions; i++)
{
tmp << "q" << i;
//tmp is now "q0"
//tmp.str() returns a 'string'

}

I'm pretty sure that works.

Or, you could do it the icky C way

int i;
char buffer[256];
for( i = 0; i < num_questions; i++)
{

memset(buffer, 0, 256); // zero out buffer
buffer[0] = 'q';
itoa(i, buffer+1, 10)
// buffer now holds "q0", and is null terminated so you can use strlen() and all that jazz


}
 

Rotorvator

Member
Jan 7, 2001
37
0
0
or, the less icky C way

int i;
char buffer[256] = {0};
for( i = 0; i < num_questions; i++)
{
::sprintf( buffer, "q%d", i );
}
 

Rotorvator

Member
Jan 7, 2001
37
0
0
Ah. I think I see what you're trying to do, and the answer is no, you can't do this in c++.

You can use the preparser ## operator to create a discrete set a la:

#define QVAR(i) q ## i

Question QVAR(1), QVAR(2), QVAR(3);

will give you variables q1, q2 and q3 of type Question.

However, the preprocessor has no looping structure per se. You might be able to do some sort
of recursion on conditional includes:

// some header ("myfile.h") file that looks like:
#if (iter < max_times )
q ## iter,
#define iter iter##1
#include "myfile.h"
#endif

and in your source do:
#define max_times 1000
#define iter 1

Question #include "myfile.h";

would expand to something like ( I haven't verified this )
Question q1, q11, q111, q1111,;

May not work ultimately work (need to get rid of the trailing comma, not sure on how gracefully the compiler will handle redef of iter),
but it would be kind of fun to see how far you could take this sort of thing.

Cheers!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126


<< Ah. I think I see what you're trying to do, and the answer is no, you can't do this in c++. >>

Yeah, you need an interpreted language like old BASIC versions, where you can build your own string of code and pass it to the interpreter to execute. Actually you can still do this with DOS batch files (.bat).

But in C/C++ the solution is either (a) use an array of class object MyClass q[10] -or- (b) use an array of _pointers_ and assign the pointers to your variables ( pQ[ 0 ] = &q1, pQ[ 1 ] = &q2, pQ[ 2 ] = &q3 ).
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |