473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Passing a structure to a function.

I am having trouble trying to display a function in my program.

The program basically opens up a binary file and reads the last name, first name, GPA etc...

What works in main is if I do a cout <<ar[i].last; The last name will display as for everything else. I need to have this work in the function but I am completly lost.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct Tstudent
{
char last[40];
char first[40];
char ID[10];
double gpa;
int score;
};

void dispStudent(Tstudent stu);

int main()
{//start main
cout << setiosflags(ios::left);
ifstream infile;
Tstudent stu;


Tstudent ar[100];

int i = 0;
infile.open("file", ios::binary);

if(infile.fail())
{
cout <<"\n\nError: FILE NOT FOUND!";
exit(1);
}

infile.read((char *) &ar[i], sizeof(Tstudent));



while(!infile.eof())
{
i++;
infile.read((char *) &ar[i], sizeof(Tstudent));

}


dispStudent(ar[i]); //problem area, not sure what to put here

infile.close();

}

/************************/

void dispStudent(Tstudent stu)
{//start main




}//end main
Apr 25 '07 #1
3 1543
nmadct
83 Expert
What you have should work, in the dispStudent function you can display the last name like this:

cout << stu.last;

However, I'd do it a little differently. When you declare the function dispStudent, instead of declaring it like this:

void dispStudent(Tstudent stu) /* passes a copy of the entire struct */

I would declare it like this:

void dispStudent(Tstudent * stu) /* passes a pointer to the struct */

Passing a pointer is much more efficient than copying the whole struct, especially when the struct is, like yours, more than trivial in size. With this declaration, the last name field can be accessed from within the dispStudent function like this:

cout << stu->last;

You can now call dispStudent like this:

Tstudent ar[100];
dispStudent(&ar[i]);
Apr 25 '07 #2
Thank you for the reply, I tried what you said just to try it and thanks!

The problem is I can't use that for my program. We have to follow the assignment :(

I have created the function and I have created the display function.

What I need to do is make it in order... I believe I need to do a while loop with the last name since the file starts with a last name.

What would be your suggestion to display the loop in ascending order.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct Tstudent
{
char last[40];
char first[40];
char ID[10];
double gpa;
int score;
};

void dispStudent(Tstudent stu);

void dispAll(Tstudent ar[], int count);

int main()
{//start main
cout << setiosflags(ios::left);
ifstream infile;
Tstudent stu;


Tstudent ar[100];

int i = 0;
infile.open("stinfo", ios::binary);

if(infile.fail())
{
cout <<"\n\nError: FILE NOT FOUND!";
exit(1);
}

infile.read((char *) &ar[i], sizeof(Tstudent));



while(!infile.eof())
{
i++;
infile.read((char *) &ar[i], sizeof(Tstudent));

}
int count;
count = i;

dispAll(ar, count);


infile.close();

}

/************************/

void dispStudent(Tstudent stu)
{//start function
cout << setiosflags(ios::left);
cout << "\nLast: " << stu.last <<" First: " <<stu.first <<" ID: " <<stu.ID;
cout << "\n\nGPA: " <<stu.gpa << " Score: " <<stu.score << endl;
cout << "\n**************************** New Entry ****************************\n";
}//end function


void dispAll(Tstudent ar[], int count)
{//start function

int i;

for(i = 0; i < (count-1); i++)
{
dispStudent(ar[i]);
}

}//end function
Apr 26 '07 #3
gpraghuram
1,275 Expert 1GB
HI,
I have one doubt in your code.
When u are reading the value from the file using the structure why are you typecasting to char* and reading.
I think it may cause you problem.

Thanks
Raghuram
Apr 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
1
by: sharadvasista | last post by:
I have a structure typedef struct t_abcd { int a; int b; int c; } abcd; I have to pass the structure to another function. I can do this in two ways.
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.