Hi All,
My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible answers:
cout << endl;
cout << "Q" << iIndex << ": " << sQuestions[iIndex].caQuestion << endl;
cout << endl;
cout << "\tA: " << sQuestions[iIndex].caPossibleAnswer1 << endl;
cout << "\tB: " << sQuestions[iIndex].caPossibleAnswer2 << endl;
cout << "\tC: " << sQuestions[iIndex].caPossibleAnswer3 << endl;
cout << "\tD: " << sQuestions[iIndex].caPossibleAnswer4 << endl;
cout << endl;
We've been told to make the code as modular as possible so I'd like to have the above code in a function but I'm not sure how to pass the question index to it (I think via a pointer but I'm unsure of the mechanics) and whether or not the function will have any visibility of the rest of the structs components... Any pointers (pun intended!) to how I can do this? Or if it can be done...
I'm also using the following code to load data into my struct. Visual C++ gives me warnings about using strcpy, is there a better way of doing this?
// load the question data (if you can figure out pointers you could put this in a function)
strcpy(sQuestions[0].caQuestion, "What statement separator is commonly used in a for loop?");
strcpy(sQuestions[0].caPossibleAnswer1, "semicolon");
strcpy(sQuestions[0].caPossibleAnswer2, "comma");
strcpy(sQuestions[0].caPossibleAnswer3, "colon");
strcpy(sQuestions[0].caPossibleAnswer4, "quote");
sQuestions[0].cCorrectAnswer = 'A';
Many thanks,
p.
- Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
e-Quals IT Practitioners Diploma - C++ Programming
Farnborough Tech. UK.
Dr. Usman Abdullahi
---------- Post updated at 06:34 PM ---------- Previous update was at 03:24 PM ----------
Hi All,
Doing some digging around I've found the answer to my problem - on page 168 of Herbert Schildt's "C++ The Complete Reference" I found a section entitled "Passing Entire Structures to Functions".
So I modified my function to look like this:
void displayQuestions(struct sQuestionFormat sQuestions, int iIndex)
{
cout << endl;
cout << "Q" << iIndex << ": " << sQuestions.caQuestion << endl;
cout << endl;
cout << "\tA: " << sQuestions.caPossibleAnswer1 << endl;
cout << "\tB: " << sQuestions.caPossibleAnswer2 << endl;
cout << "\tC: " << sQuestions.caPossibleAnswer3 << endl;
cout << "\tD: " << sQuestions.caPossibleAnswer4 << endl;
cout << endl;
}
The call to run the function from main looks like this:
displayQuestions(sQuestions[iIndex], iIndex);
And the structure is created like this:
struct sQuestionFormat sQuestions[10];