?
Solved

Structure into a Function

Posted on 2003-11-19
7
Medium Priority
?
265 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Accepted Solution

by:
SteH earned 120 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’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

650 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