Hi, Im working on a school project and could use some help. Im having trouble storing data in an array of structs. My program has to read student data from a file and store the information in a struct.

Im having trouble passing my array struct into my fuctions, and also having trouble passing my io files into and out of the functions. Here is what i have so far.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void Highest();

struct studentType
{
        string fName, lName;
        int Scores;
        int Tests;
        char grade;
};

studentType students[20];

void StuRead(&ifstream studentType students[]);   //her i got 2 errors, error: variable or field 'StuRead' declared void
error: expected primary-expression before 'studentType'

char AssignGrade(char LetterGrade, studentType students[]);
void Print(&ofstream studentType students[]);

main ()
{

ifstream inFile;
ofstream outFile;

inFile.open("Studinfo.txt");
outFile.open("Stinfo.out");


StuRead(studentType students[]);
return 0;
}


void StuRead(&inFile studentType students[]) //on this function heading I got these 2 errors, error: variable or field 'StuRead' declared void, error: redefinition of 'int StuRead'

{
int x;

x = 0;
for (x = 1; x < 20; x++)
  {
  inFile >> students[x].lName
         >> students[x].fName
         >> students[x].Tests;
  }

}




char AssignGrade(char LetterGrade, studentType students[])
{
int score, x;
score = students[x].Tests;
if(score >= 90)
        studentType.grade = 'A';
else if(score >= 80)
        studentType.grade = 'B';
else if(score >= 70)
        studentType.grade = 'C';
else if(score >= 60)
        studentType.grade = 'D';
else
        studentType.grade = 'F';
//Here i got a wierd error on each line. error: expected unqualified-id before '.' token



}



void Highest(studentType students[])
{
int x;
int highTest = 0;
        for(x = 0; x < 20; x++)
        {
          if(x > highTest)
                highTest = x;

        }


}



void Print(&outFile studentType students[])  //Here i got another error: outfile was not declared in this scope.
{
int x;

        for(x = 0; x < 20; x++)
        {
                outFile << setw(10) << students[x].tests << endl;
        }
}

As you can see, I outlined some of the error msges that i am getting but anything else that any1 finds wrong, please tell me, Thanks alot for the help guys! :)

> void StuRead(&ifstream studentType students[]);
type name comma type name comma
and so on

Lets start with an easy one

void StuRead(&ifstream studentType students[]);

The & goes after the data type (ifstream &) and you need to put a comma between the two arguments. If you're going to put the parameter name on one, why not both?
So, a corrected version of your prototype would be

void StuRead( ifstream &inFile,  studentType students[] );

Your call to the function must match the parameter list, so you must be passing an ifstream and an array of student records. When you call a function, you do not put data type names in front of the actual arguments. Nor do you put the square brackets following an array name passed as an arguments. So

StuRead(studentType students[]);

//corrected
StuRead( inFile, students );

Also, please post your code using the code tags, so that it displays better.

[code]

your code goes here

[/code]

Hey thanx alot for the tips!! Ive made some changes and been working on it for a while but im still getting some errors that im not sure what they mean. Here is my new code

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


struct studentType
{
        string fName, lName;
        int Scores;
        int Tests;
        char grade;
};

studentType students[20];

void StuRead(ifstream &inFile, studentType students[]);
char AssignGrade(char LetterGrade, studentType students[]);
void Highest(ifstream &inFile, studentType students[]);
void Print(ofstream &outFile, studentType students[]);

main ()
{
ifstream inFile;
ofstream outFile;

inFile.open("Studinfo.txt");
outFile.open("Stinfo.out");


StuRead(students);





return 0;
}


void StuRead(ifstream &inFile, students)
{
int x;

x = 0;
for (x = 1; x < 20; x++)
  {
  inFile >> students[x].lName
         >> students[x].fName
         >> students[x].Tests;
  }
}




char AssignGrade(char LetterGrade, students)
{
int score, x;

score = students[x].Tests;
if(score >= 90)
        students[x].grade = 'A';
else if(score >= 80)
        students[x].grade = 'B';
else if(score >= 70)
        students[x].grade = 'C';
else if(score >= 60)
        students[x].grade = 'D';
else
        students[x].grade = 'F';


}



void Highest(ifstream &inFile, students)
{
int x;
int highTest = 0;
        for(x = 0; x < 20; x++)
        {
          if(x > highTest)
                highTest = x;

        }


}



void Print(ofstream &outFile, students)
{
int x;

        for(x = 0; x < 20; x++)
        {
                outFile << setw(10) << students[x].Tests << endl;
        }
}

you told me not to put "studentType" before student in my function headers but im getting this error:
error: 'students' is not a type
I get this on every line that doesn't have studentType in front.

Do you have any idea about this and is there any other errors that you notice. Thanks alot!

he told you not to put the type in when you call the function not when you declare or define the function

//...
StuRead(students); // good no data type before variable
//...
void StuRead(ifstream &inFile, studentType students) // good data types befor the variables
{
//...

hope this clears it up

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.