Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structures 1

Status
Not open for further replies.

MarkJ2

Technical User
Sep 25, 2001
17
US
I need help on structures. I understand a structure definition goes in the global section of the program.

such as:

struct STUDENT
{
int id;
int midterm;
int final;
}

From there I am lost. Do you need to always declare a variable of the structure and where does that go? Also what is the syntax for declaring a variable of the structure?

Other questions....
1. How do you pass parameters ( or the fields) of a structure to a function?
2. How should the function call be written and how should the function definition be written. The book I have has no examples and I have tried numerous ways and none seem to work.
3. How exactly should the prototype declarations be written.

If someone could help me out using the example STUDENT structure above it would be appreciated.

I'll even send you a Pumpkin pie for Thanksgiving.

Thanks!
 
#include <iostream>
#include <ctime>
#include <cstdlib>

struct STUDENT
{
int iId;
int iMidTerm;
int iFinal;
};

void Print_Records ( const STUDENT &student );
void Print_Indiv( int iAttribute );
void Random_Records ( STUDENT *student );

int main()
{

STUDENT someStud;

Random_Records( &someStud );
Print_Records ( someStud );
Print_Indiv ( someStud.iId );
Print_Indiv ( someStud.iMidTerm );
Print_Indiv ( someStud.iFinal );

return 0;

}

void Random_Records( STUDENT *student )
{
srand( time( 0 ) );
student->iFinal = rand() % 100;
student->iId = rand() % 100;
student->iMidTerm = rand() % 100;
}

void Print_Records( const STUDENT &student )
{
std::cout << &quot;ID: &quot;
<< student.iId
<< std::endl
<< &quot;Mid: &quot;
<< student.iMidTerm
<< std::endl
<< &quot;Fin: &quot;
<< student.iFinal
<< std::endl;
}

void Print_Indiv( int iAttribute )
{
std::cout << iAttribute
<< std::endl;
}

The above code just shows the various ways to pass a struct and it's attributes to a function. Don't forget to compile the source with a .cpp extension. Mike L.G.
mlg400@blazemail.com
 
Along with the above, a struct can have member functions. As you are asking questions about structs, I assume you have not yet begun work on classes. Since I dont want to spoil the fun :p I will let you learn about classes first. Just remember, constructors, destructors, member-functions can all be declared within a struct, but in a struct, is is all public.

This may be a bit confusing without prior knowledge of classes BUT check back once you begin work on them. A constructor in a struct can always prove useful.

Matt
 
Of course this is just poor phrasing, but &quot;is all public&quot; -> &quot;all members are by default public&quot;. Naturally you can put in private: or protected: to change this. :) Hope that this helped! ;-)
 
True :p my english grammar is not that well before i haven't had my first cup of coffee in the morning but this is a sentence that can be picked to heck if you so wish however I have had my coffee now and know what I am typing.

Matt
 
Well I may have an advantage - here in Germany my first coffee probably comes rather earlier...:) :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top