Can anyone help me with my c++ program? Its a program I have to write using structs and arrays and i'm stuck. If anyone can help can you post back and I'll show you what i've got and maybe you can tell me why its not working correctly.

Thanks

I can try. I have not done much outside of Visual Basic. But maybe I can figure out your problem.

My assignment is to initialize an array of structs using the data from an input file. To get the input file the user must input the name of the file.Theres more i need to add but i cant get this to work so i havent even tried the other stuff.
Well this is what ive got so far...

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

using namespace std;

const int ARRAY_SIZE = 40;

struct studentType
{
string firstName;
int id;
int testScore;
};

void getData(ifstream& inData, studentType sList[], int& listSize);
void printList(const studentType sList[], int listSize) ;

int main()
{
int student_count;
studentType studentList [ARRAY_SIZE];

ifstream inData;

char inputFile[40];

cout << "Please enter the name of the input file you would like to use"
<< " and press enter"
<< endl;
cin >> inputFile;

inData.open(inputFile);

if (!inData)
{
cout << "The input file does not exist."
<< endl;

}

getData(inData, studentList, student_count);
printList(studentList, student_count);

system ("pause");
return 0;
}

void getData(ifstream& inFile, studentType sList[], int& listSize)
{
int i = 0;
cout << "getData\n";

if ( !inFile)
cout << " no File\n ";

inFile >> sList[0].firstName >> sList[0].id
>> sList[0].testScore ;

while (inFile)
{
i++;
inFile >> sList[0].firstName >> sList[0].id
>> sList[0].testScore ;
}
listSize = i ;
cout << "listSize = " << listSize <<endl <<endl <<endl;
}

void printList(const studentType sList[], int listSize)
{

First, let's go through your code and see if we can identify any issues.

1. In your code, you are declaring the array `studentList` with a constant size of `ARRAY_SIZE`. However, you are not checking if the number of students in the input file exceeds this size. You should add a check to ensure that the number of students in the file does not exceed `ARRAY_SIZE`.

2. In the `getData` function, when you iterate through the file contents, you are assigning the values to `sList[0]` every time and incrementing `i`. This means that you are only storing the values for the first student multiple times, rather than storing values for each student. Instead, you should store the values at index `i` of `sList` to keep track of different students.

Now, let's proceed to fix these issues one by one.

1. To handle the case where the number of students in the file exceeds `ARRAY_SIZE`, you can use a conditional statement inside the `while` loop in the `getData` function. Here's an example:

```cpp
while (inFile && i < ARRAY_SIZE)
{
inFile >> sList[i].firstName >> sList[i].id >> sList[i].testScore;
i++;
}
```

This will stop reading from the file if either the end of file is reached or `i` becomes equal to `ARRAY_SIZE`.

2. To store the values for each student in the `getData` function, you need to update the index of `sList` where the values are stored. Replace `sList[0]` with `sList[i]` in the `while` loop:

```cpp
inFile >> sList[i].firstName >> sList[i].id >> sList[i].testScore;
```

This will correctly store the values for each student at their respective indices in `sList`.

Once these changes are made, try running your program again to see if it works as expected.