Why am I getting these error mesages?

This is my homework.
Write a program that read data from an input file and stores the details of students in a class of size 20. The detail include Student ID, Name, and GPA. Use an array of structures to store all the data and then display them on the screen.

I use just three student first. Then if the coding (suppose) to work, then I'll change it to 20, later on.

Input File: Student.txt
0001 Brook 3.50
0002 James 3.51
0003 Katie 3.52

Supposed output:
ID   Name  GPA
0001 Brook 3.50
0002 James 3.51
0003 Katie 3.52


My code:
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
#include<iostream>
#include<fstream>
#define Size 3
using namespace std;

struct record
{
	char ID[4];
	char name[20];
	double GPA;
}
void readfromfile (record s[]);
void display (record s[]);

void main ()
{
	record student [Size];
	readfromfile (student);
	display (student);
}

void readfromfile (record s[])
{
	ifstream inputFile ("Student.txt", ios::in);
	for (int i=0; i<Size; i++)
	{
		inputFile>>s[i].ID>>s[i].name>>s[i].GPA;
	}
	inputFile.close();
}

void display (record s[])
{
	cout<<"ID\tName\tGPA\n";
	for (int i=0; i<Size; i++)
	{
		cout<<s[i].ID<<"\t";
		cout<<s[i].name<<"\t";
		cout<<s[i].GPA<<endl;
	}
}


The error:
: error C2628: 'record' followed by 'void' is illegal (did you forget a ';'?)
: error C2556: 'void readfromfile(record [])' : overloaded function differs only by return type from 'record readfromfile(record [])': see declaration of 'readfromfile'
: error C2371: 'readfromfile' : redefinition; different basic types: see declaration of 'readfromfile'

Build FAILED.

Time Elapsed 00:00:01.65
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You forgot a ; on line 11, after the definition of your struct.
: error C2628: 'record' followed by 'void' is illegal (did you forget a ';'?)

You need a semi-colon after a struct definition.

1
2
3
4
struct foo
{
    ...
}; //semi-colon goes here 
Last edited on
Thanks firedraco and shacktar!
But, I got a new problem.

The output is different:
ID     Name     GPA
0001Brook     Brook     3.50
0002James     James     3.51
0003Katie     Katie     3.52


Why is this happen? :(
You need to add one more character to the ID field to allow room for the null character.

1
2
3
4
5
struct record
{
	char ID[5];
	...
};
Thanks shacktar! May god bless you! :)
Topic archived. No new replies allowed.