Solved

Structure into a Function

Posted on 2003-11-19
7
261 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

685 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