By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

how to pass a struct to a function

P: 57
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
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
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
Expert 2.5K+
P: 3,652
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

100+
P: 147
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

P: 57
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
Expert 2.5K+
P: 3,652
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

Post your reply

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