Solved

Structure into a Function

Posted on 2003-11-19
7
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

707 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