Solved

Structure into a Function

Posted on 2003-11-19
7
259 Views
Last Modified: 2010-04-01
Im trying to pass all the data in from the variable Database[i] after it has all the data in each array element. Does anybody know how I can pass the variable Database with the information already in it to a function? my sample code is below thank you.
---------------------------------------------
struct record{
      int key;
      string firstName;
      string lastName;
      int studentid;
      int courseID;
      string coursename;
      double courseMark;
};



string Getfirstname();
string Getlastname();
int Getstudentid();
int Getcourseid();
string Getcoursename();
double Getcoursemark();


int main(void)
{
      
      record Database[1000];//reserve memory for 100 records
      int i=0;
      double sum=0;
      char reply;

      do{
                  
      Database[i].key=i;
      Database[i].firstName=Getfirstname();      
      Database[i].lastName=Getlastname();      
      Database[i].studentid=Getstudentid();
      Database[i].courseID=Getcourseid();
      Database[i].coursename=Getcoursename();
      Database[i].courseMark=Getcoursemark();
      i++;
      cout<<"Would you like to enter another student in the database? (y) or (n) "<<endl;
      cout<<"Response: ";
      cin>>reply;
      
      
      }while(reply=='y');
----------------------------------------
so I want to put database with all the elements of the array filled into a function thank you.
0
Comment
Question by:fluked
7 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9780081
Do you want only one set inside the function?

fun (Database[i])

or all?

fun2 (Database)

are the calls to the function. The declaration needs to know the type of the variable Database.
Assuming Database is of type

MyData Database[100];

Then the declaration is

fun (MyData& singleData) ;

or

fun2 (MyData[]& allData);
0
 
LVL 13

Expert Comment

by:SteH
ID: 9780091
BTW this was assuming references. With pointers the above changes to
fun (MyData* singleData);

fun (&Database[i])

fun2 (MyData** allData);

fun (&Database)

Calls could be different if you don't need to modify the struct.
0
 
LVL 1

Author Comment

by:fluked
ID: 9780092
I want to be able to import the Database array into a function. After the do-while loop has finished. Do you have an example ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
SteH earned 30 total points
ID: 9780251
I read your code a bit more careful:

void MyFunc (record* pMyDB); //

void MyFunc (record* pMyDB) {
      cout << pMyDB[i].firstname << " " << pMyDB[i].lastname << endl;
}
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9780298
Hi

You can pass a pointer to the database array into the function. This is the best way to do it.

It can be done as follows:

func1(record *);           //Function declaration

Now for function definition (You will write the function as)

func1(record *databasearray)
{
         //Write function code here
}

Now after the do-while loop you can call the function as:

func1(Database);                          //Database is base address of array to be passed

Then inside the function you can use 'databasearray' just as a normal array.

Dhyanesh

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249311
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: SteH {http:#9780251}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 120
C++ Language error 28 197
Unable to start eclipse ? 17 134
VS2015 Redefinition errors 4 30
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now