The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers > Homework & Coursework Questions
.
google unix.com



Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pointer to a struct (with pointers) *** glibc detected *** double free jatoo High Level Programming 1 12-05-2008 08:31 AM
Struct Array micmac700 High Level Programming 1 12-13-2006 01:07 PM
struct tm problem bankpro High Level Programming 4 01-20-2006 06:51 AM
save a struct kall_ANSI High Level Programming 2 10-29-2004 11:42 AM
Struct Initialization amatsaka High Level Programming 4 12-20-2002 10:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
pondlife pondlife is offline
Registered User
  
 

Join Date: May 2004
Location: UK
Posts: 85
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];

Last edited by pondlife; 2 Weeks Ago at 11:45 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0