Help on inputting info into struct arrays from external file

Please someone, I'm so lost just let me know wtf to put into the if statement or how I can repeatedly insert information into the struct data arrays, I feel like I've tried everything and it's just giving me "expected primary expression" over and over again :(

Will check this post in the morning thank you for everyone who attempts to help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 20;

enum programType {CSCI, DBMS, INFM, SDEV};

struct nameType
{
    string firstName;
    char middleInitial;
    string lastName;
};

struct studentType
{
    nameType name;
    nameType ID;
    nameType email;
    int GPA;
    programType program;
};

void readClassRoster(ifstream&, studentType[], int&);
void readProgramGPA(ifstream&, studentType[], int);
int findStudentByID(int, const studentType[], int);
double findHighestGPA(const studentType[], int);
void printHighestGPA(double, const studentType[], int);

int main()
{
    
	
	return 0;
}

void readClassRoster(ifstream&, studentType[], int&)
{
    ifstream in;
    in.open("classroster.txt");
    
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        
    }
    
}
Hi

In you function definition on line 40, you need to provide names for the function parameters. I do this for the declaration as well - it is useful for humans to see what is going on, a name for the parameter helps a lot as to what it means. At the moment I don't know / shouldn't have to guess what that int reference is for.

The ifstream on line 42 is that unnecessary - just use the first parmeter instead but with a name

You should check that opening the file worked.
Why is ID and email of type nameType?

feel like I've tried everything


but you haven't posted any code that tries to read the file - and no sample file data.

Post what you;re tried to read the file and a sample of the file contents.
Hello domweng,

I have put some comments in the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 20;
//constexpr int ARRAY_SIZE{ 20 };  // <--- C++11 on updated version.

const enum programType { CSCI, DBMS, INFM, SDEV };  // <--- As a global variable it should be a constant.

struct nameType
{
    string firstName;
    char middleInitial;
    string lastName;
};

struct studentType
{
    nameType name;   // <--- "name" can hold a first name, middle initial and a last name.
    nameType ID;     // <--- "ID" can hold a first name, middle initial and a last name. Probably should just be a "std::string". Or could be a numeric variable.
    nameType email;  // <--- "email" can hold a first name, middle initial and a last name. Probably should just be a "std::string".
    int GPA;
    programType program;
};

void readClassRoster(ifstream&, studentType[], int&);  // <--- As per the TheIdeasMan variable names here do help, but not required.
void readProgramGPA(ifstream&, studentType[], int);
int findStudentByID(int, const studentType[], int);
double findHighestGPA(const studentType[], int);
void printHighestGPA(double, const studentType[], int);

int main()  // <--- "main" is empty. Program does nothing.
{


    return 0;  // <--- Not required, but makes a good break point for testing.
}

void readClassRoster(ifstream&, studentType[], int&)  // <--- As per the TheIdeasMan needs variable names.
{
    ifstream in;  // <--- Why pass a file stream if you are going to open a new one?

    in.open("classroster.txt");
    // <--- Need to check if the file is open and usable.

    for (int i = 0; i < ARRAY_SIZE; i++)
    {

    }
}

For the struct "nameType" consider "StudentName". It is more descriptive of what it is. And for "studentType" consider "StudentInfo". A good name will help to spot errors or problems.

Since the function "readClassRoster" is where you read the file this is where you should define the file stream. You really do not need to be passing a file stream to the function.

When dealing with a file I like to use this. It may be a little more than you need, but I found it good when first learning.
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "" };  // <--- Put File name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
    std::cout << "\n     File " << std::quoted(inFileName) << " did not open.\n";  // <--- Requires header file "<iomanip>".
    //std::cout << "\n     File \"" << inFileName << "\" did not open.\n";  // <--- Does the same as above.

    return 1;
}

Since this should be in the function the function would need to return an "int". And if everything work then the function should return 0; before the closing brace.

In "main" when you call the function I do this:
1
2
if (responce = readClassRoster(parameters need changed))
    return responce;


Last point: if your program uses an input file ALWAYS include the input file or at least a fair sample and if there is something in that file causing a problem make sure it is in the sample. This way everyone will have the same file to use and there is no guess work involved.

Andy
Topic archived. No new replies allowed.