473,506 Members | 13,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass a struct to a function

57 New Member
can I pass grades.projects in my function call that is void get_scores(ifstream& infile, int num_scores, grades.projects)

and the function would look like
void get_scores(ifstream& infile, int num_scores, records& grades)
{
in_file >> grades.name;

for (int i = 0; i < num_scores; i++)
{
in_file >> records grades[i];

}

I was going to try something like this but Im not sure this would work. And I still have a couple errors I cant fix
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. ifstream in_file;
  9. //structure that holds student record information.
  10. struct records
  11. {
  12.         string name;
  13.         float quizes[7];
  14.         float projects[6];
  15.         float exams[2];
  16.         float labs[14];
  17. };
  18.  
  19. const int num_quizes = 7;
  20. const int num_exams = 2;
  21. const int num_projects = 6;
  22. const int num_labs = 14;
  23.  
  24.  
  25. const int quizes_pts = 70;
  26. const int exams_pts = 200;
  27. const int projects_pts = 300;
  28. const int labs_pts = 150;
  29.  
  30. void get_scores(ifstream & infile, int num_scores, records& grades);
  31. void get_avearage(records grades, int num_scores,int tot_pts,float& average,string);
  32.  
  33. int main()
  34. {
  35.         // Ask the user for a file name
  36.         cout << "Enter a file name: ";
  37.         string file;
  38.         cin >> file;
  39.         cout << file;
  40.  
  41.         int counter = 1;
  42.  
  43.  
  44.         records grades;
  45.         // Define a file input stream and open the file
  46.         in_file.open("file.txt"); //opening file
  47.  
  48.         if (in_file.fail())
  49.         {
  50.         // Failed to open the file (file doesn't exist or isn't readable)
  51.         cout << "Could not open file: " << file  << "\n";
  52.         exit(1);
  53.         }
  54.  
  55.  
  56.         float quiz_ave = 0.0;
  57.         float exam_ave = 0.0;
  58.         float project_ave = 0.0;
  59.         float lab_ave = 0.0;
  60.  
  61.         // Repeatedly get characters from the file
  62.  
  63.  
  64.         void get_scores(ifstream& infile, int num_quiz, records& grades.quizes);   //function for inputing info in struct.
  65.  
  66.         void get_avearage(records grades.quizes, int num_quizes, int quizes_pts, float& quiz_ave, "quiz"); //function for average
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     // Close the file
  73.     in_file.close();
  74.  
  75.  
  76.     return 0;
  77. }
  78.  
  79. //function that takes in parameters to input scores in array
  80. void get_scores(ifstream& infile, int num_scores, records& grades)
  81. {
  82.         in_file >> grades.name;
  83.  
  84.         for (int i = 0; i < num_scores; i++)
  85.         {
  86.          in_file >> records grades[i];
  87.  
  88.         }
  89.  
  90. }
  91.  
  92. //funtion that takes the average of the inputed scores
  93. void get_avearage(records grades, int num_scores,int tot_pts,float& average,string name)
  94. {
  95.         if (!("name" == "projects"))
  96.         {
  97.         float a;
  98.         for (int i = 0; i < num_scores; i++)
  99.         {
  100.                 records grades[i] += a;
  101.         }
  102.                 average = (a/tot_pts);
  103.  
  104.         else
  105.                 float b;
  106.                 record grades[0] = b;
  107.                 average = (b/20);
  108.  
  109.                 for (int i = 1; i < num_scores; i++)
  110.                 {
  111.                         float c;
  112.                         records grades[i] += c;
  113.                 }
  114.                 ave_1 = (c/(tot_pts - 20));
  115.                 average += ave_1;
  116.         }
  117. }
  118.  
  119.  
  120. }
main.cpp: In function `int main()':
main.cpp:63: error: expected `,' or `...' before '.' token
main.cpp:65: error: expected `,' or `...' before '.' token


main.cpp: In function `void get_scores(std::ifstream&, int, records&)':
main.cpp:85: error: expected primary-expression before "grades"
main.cpp:85: error: expected `;' before "grades"
main.cpp: In function `void get_avearage(records, int, int, float&, std::string)':
main.cpp:99: error: expected initializer before '+=' token
main.cpp:103: error: expected primary-expression before "else"
main.cpp:103: error: expected `;' before "else"
main.cpp:105: error: `record' was not declared in this scope
main.cpp:105: error: expected `;' before "grades"
main.cpp:106: error: `b' was not declared in this scope
main.cpp:111: error: expected initializer before '+=' token

can anyone help?
May 11 '07 #1
5 3859
gpraghuram
1,275 Recognized Expert Top Contributor
Hi
You have to precede the struct declaration with keyword struct ...
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.         // Ask the user for a file name
  4.         cout << "Enter a file name: ";
  5.         string file;
  6.         cin >> file;
  7.         cout << file;
  8.  
  9.         int counter = 1;
  10.  
  11.  
  12.         struct records grades; //add the struct keyword whereevr u 
  13.                                          //use  structure or typdef it.
  14.  
  15.  
Thanks
Raghuram
May 11 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Hi
You have to precede the struct declaration with keyword struct ...
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.         // Ask the user for a file name
  4.         cout << "Enter a file name: ";
  5.         string file;
  6.         cin >> file;
  7.         cout << file;
  8.  
  9.         int counter = 1;
  10.  
  11.  
  12.         struct records grades; //add the struct keyword whereevr u 
  13.                                          //use  structure or typdef it.
  14.  
  15.  
Thanks
Raghuram
Not in C++, and from the header files without the .h I assume this is a C++ question.

You should definitely be able to write a function that will accept a struct as an argument - however, you wouldn't be passing grades.projects. The projects member of grades is a float array, not a struct variable. Just pass grades and it should work.
May 11 '07 #3
scruggsy
147 New Member
To typedef it:
Expand|Select|Wrap|Line Numbers
  1. typedef struct{
  2.         string name;
  3.         float quizes[7];
  4.         float projects[6];
  5.         float exams[2];
  6.         float labs[14];
  7. } records;
  8.  
  9. ...
  10. records grades; // this works now without requiring 'struct' keyword
May 11 '07 #4
gdarian216
57 New Member
Not in C++, and from the header files without the .h I assume this is a C++ question.

You should definitely be able to write a function that will accept a struct as an argument - however, you wouldn't be passing grades.projects. The projects member of grades is a float array, not a struct variable. Just pass grades and it should work.

so if im trying to use a function that will take input from a file and store it in the different arrays in my struct would I pass grades quizes or how would I do that?
May 11 '07 #5
Ganon11
3,652 Recognized Expert Specialist
It depends on how you want your function to work. I can think of two ways:

1) Have the function input every single array at once. In this case, I'd use the actual struct variable as an argument, and then read into grades.quizzes, grades.projects, etc. This is what I'd prefer to do, myself.

2) Have the function read in only a single array, and call it several times for each struct variable. In this case, I'd use a float array as an argument, as well as an additional int argument for the size of the array to be filled. You would pass this function grades.quizzes, grades.projects, etc. individually.
May 11 '07 #6

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

Similar topics

8
1558
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
6
5547
by: yezi | last post by:
Hi: How to code for operation between 2 structures? The structure's defination is : struct sockaddr_in my_addr; I need call a subfunction and pass the value in my_addr to the subfuncition...
2
5222
by: prakashgkhaire | last post by:
i have two structure where first structure consists val and structure pointer(linklist), 2nd structure consists, val, a varialbe of first structure, pointer of structure. Now i found to pass the...
17
3375
by: Jellicle | last post by:
There are some sturct arrays (or struct vectors), such as: struct S1_t { double keyField ; ..// other fields } S1_t s1;
14
20369
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
28
4671
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
12
3827
by: Fred | last post by:
Is it possible to create a function which will take any number of structures as an argument? For example, what if I wanted depending on the circumstances to pass a structure named astruct, or...
2
3001
by: guy.gorodish | last post by:
hi, i have couple of qeustions: 1. I have built a interop of c dll. In this dll i have a function that expect a pointer to a struct. I want to pass a struct from my c# interop into this c...
3
2508
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
4
6654
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is within the addStudent() function. I am able to...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.