Reading Infile to Struct

Hi. I am pretty new to C++ and have gotten pretty stuck. I have searched in some books and forum and have not been able to find a solution (which leads me to believe I'm looking for the wrong info or I have the wrong question). I appreciate your help in advance.

I have a .txt file with 20 student's first names, last names, and grades.
I am trying to place it into a struct.
I can get the .txt file to open but I am unable to get the data to be read into the struct. Is there an intermidary step I'm missing?
Here is the code snippet.
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


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

using namespace std;

 struct gradebook
 {
 char first_name [20];
 char last_name;
 int grade;
 };
 
 int main ()
{
const int maxvalue = 20;
int counter;
int i=0, j;

ifstream infile;
ofstream outfile;  
string inputFile;
string outputFile;


ifstream infile("namegrade");

if (!infile)
{
	cout <<"Unable to open files."<<endl;
	cout <<"End of program"<<endl;
	return 1;
	}
cout <<"File opened"<<endl;

for (counter =0; counter <20; counter++)
{
    infile >> gradebook[counter].first_name
           >> gradebook[counter].last_name
           >>gradebook[counter].grade;

//This is where I'm getting pretty stuck. 


Thank you in advance.
You say it's a .txt file, but you're trying to open a different file with no extension: "namegrade". It should be "namegrade.txt".

EDIT: you're also redefining infile in line 30.
Last edited on
Thank you Filipe. I tried again and am getting compiling errors where the data is supposed to go into the struct gradebook.

Here is the new code. I appreciate your 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

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


 struct gradebook
 {
 char first_name;
 char last_name;
 int grade;

 };
  
 int main ()
{
int counter;

ifstream infile;
ofstream outfile;
string inputFile;
string outputFile;


infile.open("namegrade.txt");
outfile.open("completedocument.txt");
if (!infile)
{
	cout <<"Unable to open files."<<endl;
	cout <<"End of program"<<endl;
	return 1;
	}
	

cout <<"File opened"<<endl;

for (counter =0; counter <20; counter++)
{
    infile >> gradebook[counter].first_name
           >> gradebook[counter].last_name
           >>gradebook[counter].grade;

//I'm getting the error " expected primary expression before '[]' token after the
// infile>>gradebook[counter].fist_name code. 


I really appreciate help.
gradebook is the name of a type you created, just like int is the name of a built-in type. It looks like you want to instantiate an array of 20 gradebooks. You could do it like this:

gradebook grades[20];

and then remember you need to access grades[counter] instead of what you're using. But you really should use a std::vector. I suggest you look it up.
Thank you. I appreciate your help.
Topic archived. No new replies allowed.