C++ struct pointers & functions

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions C++ struct pointers & functions
# 1  
Old 11-06-2009
C++ struct pointers & functions

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:

Code:
		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?

Code:
	// 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.


4. 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:



Code:
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:

Code:
		displayQuestions(sQuestions[iIndex], iIndex);

And the structure is created like this:

Code:
struct sQuestionFormat sQuestions[10];

Smilie

Last edited by pondlife; 11-06-2009 at 11:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

2. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

3. UNIX and Linux Applications

nohup and & versus functions

when i have a function definition and function call in my script , i am unable to run my script in background with nohup.. Help me out please..... (3 Replies)
Discussion started by: venugopalsmartb
3 Replies

4. Programming

Passing an instance of struct to functions in other src files

I am trying to work out the best syntax for a relatively simple operation. The goal is to declare an instance of a struct and pass it around to be populated and have the data manipulated. There is an extra wrinkle in that the functions are in different src files. The main is simple, #include... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

5. Homework & Coursework Questions

Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Discussion started by: ignatius3
2 Replies

6. Programming

functions that manipulate void pointers

I have two or more linked lists that have the same names for their "next". For example, struct server_t { sockaddr_in * sin; server_t * next_; } struct player_t { char name; player_t * next_; } How can I get a function to take in either type and manipulate the pointers? I... (3 Replies)
Discussion started by: pyramation
3 Replies

7. Solaris

encryption & decryption functions in sun solaries

hi, is there any library functions available in sun solaries for encryption and decryption functions. regards suresh (1 Reply)
Discussion started by: suresh_rtp
1 Replies

8. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

9. Shell Programming and Scripting

a script to clone a dir tree, & overwrite the dir struct elsewhere?

hi all, i'm looking for a bash or tcsh script that will clone an empty dir tree 'over' another tree ... specifically, i'd like to: (1) specify a src directory (2) list the directory tree/hiearchy beneath that src dir, w/o files -- just the dirs (3) clone that same, empty dir hierarchy to... (2 Replies)
Discussion started by: OpenMacNews
2 Replies

10. UNIX for Advanced & Expert Users

vi search & replace functions

I'm trying to do a global search and replace in vi. I am trying to replace a string, call it "BOB" with a carriage return and can't seem to find a reference to it. Command syntax s%/BOB/???/g What would I substitute the "???" with? (7 Replies)
Discussion started by: barnettdk
7 Replies
Login or Register to Ask a Question