read from file using functions

Hi guys, I'm trying to write a program that has a menu for displaying data from an existing file in different ways. So far I've only worked on menu option one and I'm stuck. For some reason the compiler doesn't like my prototype for the ReadFile function. I'm getting this error below. Any ideas?

Error: variable or field 'ReadFile' declared void

Example file I'm trying to read from:
Terri Pinon 95 A
John Doe 80 B
Jack Johnson 75 C
Natalie Smith 90 A
Chris Shaw 70 C
Tim Thomas 100 A
Corey Jones 92 A
Anthony Hill 85 B
Jerry Culpepper 86 B
Bill Moon 73 C
Jen Lowell 70 C
Nick Brown 99 A
Timothy Bell 89 B
John Mallek 50 F
Clint Eastwood 68 D
Michelle Norris 84 B
Sally Fields 96 A
Jason Bourne 100 A
Melody Swift 83 B
Thomas Iban 70 C

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

const int SIZE = 20;                     
typedef char String[SIZE];

//prototypes
void Menu(int &);
void ReadFile(fstream &,String[],String[],int[],char[]);
void ShowContents(fstream &,String[],String[],int[],char[],const int);

using namespace std;

int main()
 {  
    char filename[]= "grades.txt";
    String fname[SIZE], lname[SIZE];
    int score[SIZE];
    char grade[SIZE];
    int choice;
    
    ifstream InGradeList;
    ofstream OutGradeList
    InGradeList.open(filename);
    OutGradeList.open(filename);
    
    
    if(InGradeList.good() && OutGradeList.good())
    {
         Menu(choice); 
         switch(choice)
        	{
            case 1:  
                   ReadFile(InGradeList,fname,lname,score,grade);
                   ShowContents(OutGradeList,fname,lname,score,grade,SIZE);
            /*case 2:
            
            case 3:
             
            case 4:
            
            case 5:*/
            }
                                   
    }
	else
         cout << "File did not open successfully." << endl << endl;
          
          
    system("PAUSE");
    return EXIT_SUCCESS;

}
 

    void Menu(int &choice) 
    { 
         cout << "\n    Choose an option:";
	     cout << "\n...................................";
         cout << "\n 1- Display All Content of the File";
         cout << "\n 2- Display Content in Reverse Order";
         cout << "\n 3- Display from Point A to Point B";
         cout << "\n 4- Display from Point B to Point A";
         cout << "\n 5- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 
    

    void ReadFile(fstream &inFile,String fname[],String lname[],int score[],char grade[])
    {
        int idx = 0; 
        while (inFile.good())
        {
         inFile >> fname[idx] >> lname[idx] >> score[idx] >> grade[idx];
         idx++;
        }
        inFile.close();
     }
                      

    void ShowContents(fstream &outputFile,String fname[],String lname[],int score[],char grade[],const int SIZE)
    {
        int idx = 0; 
        for(int idx = 0; idx < SIZE; idx++)
         {
              outputFile << fname[idx] << " " << lname[idx] << " ";
              outputFile << score[idx] << " " << grade[idx] << endl;
         }

        outputFile.close();
     }
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>
using namespace std;

const int SIZE = 20;                     

//prototypes
void Menu(int &);
void ReadFile(ifstream &inFile,string fname[],string lname[],int score[],char grade[]);
void ShowContents(string fname[],string lname[],int score[],char grade[],const int);

int main()
 {  
    string filename = "grades.txt";
    string fname[SIZE], lname[SIZE];
    int score[SIZE];
    char grade[SIZE];
    int choice;
    string temp;
    
	ifstream InGradeList;
    InGradeList.open(filename);

	if (InGradeList.fail())
	{
		cerr << filename << "File failed to open" << endl;
		cin.ignore();
		exit(1);
	}
    
         Menu(choice); 
         switch(choice)
        	{
            case 1:  
				ReadFile(InGradeList,fname,lname,score,grade);
				ShowContents(fname,lname,score,grade,SIZE);
				break;
            /*case 2:
            
            case 3:
             
            case 4:
            
            case 5:*/
				default:
				cout << "Invalid input" << endl << endl;
			}
	
	InGradeList.close();    
    system("PAUSE");
    return 0;

}

	void Menu(int &choice) 
    { 
         cout << "\n    Choose an option:";
	     cout << "\n...................................";
         cout << "\n 1- Display All Content of the File";
         cout << "\n 2- Display Content in Reverse Order";
         cout << "\n 3- Display from Point A to Point B";
         cout << "\n 4- Display from Point B to Point A";
         cout << "\n 5- Exit";
	     cout << "\n\nEnter your choice: ";
	     cin >> choice; 
    } 
    

   void ReadFile(ifstream &inFile,string fname[],string lname[],int score[],char grade[])
    {
        int idx = 0; 
        while (!inFile.eof())
        {
			inFile >> fname[idx] >> lname[idx] >> score[idx] >> grade[idx];
			idx++;
        }
	}
                      

    void ShowContents(string fname[],string lname[],int score[],char grade[],const int)
	{
		int idx = 0; 
        for(int idx = 0; idx < SIZE; idx++)
         {
             cout << fname[idx] << " " << lname[idx] << " ";
             cout << score[idx] << " " << grade[idx] << endl;
         }
	}
Last edited on
thanks Yanson! Was it just the positioning of namespace?
I tried to compile the code that you sent, but I'm still getting an error, just not in the same place at least, so that's progress I guess. :) Were you able to compile? I always worry it's the compiler I'm using. Dev-C++.

Line 26 error: no matching function for call to 'std::basic_ifstream<char,....."
Topic archived. No new replies allowed.