Solved

Structure into a Function

Posted on 2003-11-19
7
258 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at 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 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.

746 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

14 Experts available now in Live!

Get 1:1 Help Now