Errors in file handling code

Hello everyone,
I am taking my first class in C++ and I have run into some errors while trying to write a file handling and application program. I thought I was doing good until I ran the compiler. The first error appears on the line (84) second on line (91). I read my book and tried to write the code. Could someone tell me if I converted the pseudocode correctly to source code for these two lines (84) and (91)? I see errors 110, 116, 122. I don't understand what is meant by "no match" or how to fix it. Any help would be greatly appreciated. Thank you for your time.



84 expected unqualified-id before '(' token
91 expected unqualified-id before '(' token
110 no match for 'operator!=' in 'inFile1 != -0x000000001'
116 no match for 'operator!=' in 'inFile2 != -0x000000001'
122 no match for 'operator==' in 'inFile1 == -0x000000001'
122 no match for 'operator==' in 'inFile2 == -0x000000001'

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Start
//     Declarations
//         InputFile inFile1;
//         InputFile inFile2;
//         OutputFile outFile1;
//         num mClientNumber, fClientNumber
//         string mClientName, fClientName
//         bool atLeastOneFileNotAtEnd = true
//         bool inFile1Written = false
//         bool inFile2Written = false
//     output "File merge processing starting"
//     open inFile1 "MaleClients.rtf"
//     open inFile2 "FemaleClients.rtf"
//     open outFile1 "MergedClients.rtf"
//     read mClientNumber and mClientName from inFile1
//     read fClientNumber and fClientName from inFile2
//     while ( atLeastOneFileNotAtEnd == true )
//       if (inFile1 is EOF)
//         if (inFile2Written == false)
//           output fClientNumber, fClientName to Outputfile
//           inFile2Written = true
//         endif
//       else if (inFile2 is EOF)
//         if (inFile1Written == false)
//           output mClientNumber, mClientName to Outputfile
//           inFile1Written = true
//         endif
//       else if (mClientNumber < fClientNumber)
//         output mClientNumber, mClientName to Outputfile
//         inFile1Written = true
//       else
//         output fClientNumber, fClientName to Outputfile
//         inFile2Written = true
//       endif
//
//
//       if ((inFile1 not EOF) AND (inFile1Written == true))
//         read mClientNumber and mClientName from inFile1
//         inFile1Written = false
//       endif
//       if ((inFile2 not EOF) AND (inFile2Written == true))
//         read fClientNumber and fClientName from inFile2
//         inFile2Written = false
//       endif
//       if ((inFile is EOF) AND (inFile2 is EOF))
//         atLeastOneFileNotAtEnd = false
//       endif
//     endwhile
//     close inFile1
//     close inFile2
//     close outFile1
//     output "Merging Complete"
// Stop

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
    ifstream inFile1;
    ifstream inFile2;
    ofstream outFile1;
    int mClientNumber, fClientNumber;
    string mClientName, fClientName;
    bool atLeastOneFileNotAtEnd = true;
    bool inFile1Written = false;
    bool inFile2Written = false;
    
    cout << "File merge processing starting" << endl;
    
    inFile1.open("MaleClients.rtf");
    inFile2.open("FemaleClients.rtf");
    outFile1.open("MergeClients.rtf");
    
    inFile1 >> mClientNumber;
    inFile1 >> mClientName;
    inFile2 >> fClientNumber;
    inFile2 >> fClientName;
    while(atLeastOneFileNotAtEnd == true)
     { 
      if(!(inFile1.EOF()))
        if (inFile2Written == false)
        {
                    outFile1 << fClientNumber << endl;
                    outFile1 << fClientName << endl;
                    inFile2Written = true;
        }
      else if (!(inFile2.EOF()))
        if (inFile1Written == false)
        {
                    outFile1 << mClientNumber << endl;
                    outFile1 << mClientName << endl;
                    inFile1Written = true;
        }
      else if (mClientNumber < fClientNumber)
        {
              outFile1 << mClientNumber << endl;
              outFile1 << mClientName << endl;
              inFile1Written = true;
        }
      else
        {
              outFile1 << fClientNumber << endl;
              outFile1 << fClientName << endl;
              inFile2Written = true;
        }
      if ((inFile1 != EOF) && (inFile1Written == true))
        {
                   inFile1 >> mClientNumber;
                   inFile1 >> mClientName;
                   inFile1Written = false;
        }
      if ((inFile2 != EOF) && (inFile2Written == true))
        {
                   inFile2 >> fClientNumber;
                   inFile2 >> fClientName;
                   inFile2Written = false;
        }
      if ((inFile1 = EOF) && (inFile2 = EOF))
        {
                   atLeastOneFileNotAtEnd = false;
        }
       
     }
    inFile1.close();
    inFile2.close();
    outFile1.close();
    cout << "Merging Complete" << endl;
    system("PAUSE");
    return 0;                                          
}
You may not use operator != for operands of type ifstream and int. So the following statements are incorrect

if ((inFile1 != EOF) && (inFile1Written == true))
if ((inFile2 != EOF) && (inFile2Written == true))

Also you may not assign an object of type ifstream an object of type int. So this statement is also invalid

if ((inFile1 = EOF) && (inFile2 = EOF))
Thanks for looking at the code but I am totally confused. Could you please clarify. I am not sure how to rewrite it. Also do you know why I am get this error code "expected unqualified-id before '(' token " on the lines (84) and (91). Thanks for your time.
Is there anyone that could help me?
I believe that the .EOF() that you're using in lines 84 and 91 is not actually a function, but rather a variable, so just remove the () and you should be good.

Edit: As for the other problems, instead of putting inFile1 != EOF, put !inFile1.EOF. Change every case appropriately and it should work.
Last edited on
Thanks for looking at the code. I have change my source code to the following but I am still receiving the following error. I have tried all that I know and can't seem to get it right. Thanks again for your time.


30 expected unqualified-id before '(' token
37 expected unqualified-id before '(' token
56 expected unqualified-id before '(' token
62 expected unqualified-id before '(' token
68 expected unqualified-id before '(' token
68 expected unqualified-id before '(' token


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
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
    ifstream inFile1;
    ifstream inFile2;
    ofstream outFile1;
    int mClientNumber, fClientNumber;
    string mClientName, fClientName;
    bool atLeastOneFileNotAtEnd = true;
    bool inFile1Written = false;
    bool inFile2Written = false;
    
    cout << "File merge processing starting" << endl;
    
    inFile1.open("MaleClients.rtf");
    inFile2.open("FemaleClients.rtf");
    outFile1.open("MergeClients.rtf");
    
    inFile1 >> mClientNumber;
    inFile1 >> mClientName;
    inFile2 >> fClientNumber;
    inFile2 >> fClientName;
    while(atLeastOneFileNotAtEnd == true)
     { 
      if(!(inFile1.EOF()))
        if (inFile2Written == false)
        {
                    outFile1 << fClientNumber << endl;
                    outFile1 << fClientName << endl;
                    inFile2Written = true;
        }
      else if (!(inFile2.EOF()))
        if (inFile1Written == false)
        {
                    outFile1 << mClientNumber << endl;
                    outFile1 << mClientName << endl;
                    inFile1Written = true;
        }
      else if (mClientNumber < fClientNumber)
        {
              outFile1 << mClientNumber << endl;
              outFile1 << mClientName << endl;
              inFile1Written = true;
        }
      else
        {
              outFile1 << fClientNumber << endl;
              outFile1 << fClientName << endl;
              inFile2Written = true;
        }
      if (!(inFile1.EOF) && (!inFile1Written))
        {
                   inFile1 >> mClientNumber;
                   inFile1 >> mClientName;
                   inFile1Written = false;
        }
      if (!(inFile2.EOF) && (!inFile2Written))
        {
                   inFile2 >> fClientNumber;
                   inFile2 >> fClientName;
                   inFile2Written = false;
        }
      if ((!inFile1.EOF) && (!inFile2.EOF))
        {
                   atLeastOneFileNotAtEnd = false;
        }
       
     }
    inFile1.close();
    inFile2.close();
    outFile1.close();
    cout << "Merging Complete" << endl;
    system("PAUSE");
    return 0;                                          
}
to check not end of file, the function should be written as
if(!infile.eof())

Dont use CAPS for eof and dont use unnecessary brackets.

After altering, you code will be like this
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

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
    ifstream inFile1;
    ifstream inFile2;
    ofstream outFile1;
    int mClientNumber, fClientNumber;
    string mClientName, fClientName;
    bool atLeastOneFileNotAtEnd = true;
    bool inFile1Written = false;
    bool inFile2Written = false;
    
    cout << "File merge processing starting" << endl;
    
    inFile1.open("MaleClients.rtf");
    inFile2.open("FemaleClients.rtf");
    outFile1.open("MergeClients.rtf");
    
    inFile1 >> mClientNumber;
    inFile1 >> mClientName;
    inFile2 >> fClientNumber;
    inFile2 >> fClientName;
    while(atLeastOneFileNotAtEnd == true)
     { 
      if(!inFile1.eof())
        if (inFile2Written == false)
        {
                    outFile1 << fClientNumber << endl;
                    outFile1 << fClientName << endl;
                    inFile2Written = true;
        }
      else if (!inFile2.eof())
        if (inFile1Written == false)
        {
                    outFile1 << mClientNumber << endl;
                    outFile1 << mClientName << endl;
                    inFile1Written = true;
        }
      else if (mClientNumber < fClientNumber)
        {
              outFile1 << mClientNumber << endl;
              outFile1 << mClientName << endl;
              inFile1Written = true;
        }
      else
        {
              outFile1 << fClientNumber << endl;
              outFile1 << fClientName << endl;
              inFile2Written = true;
        }
      if (!inFile1.eof() && !inFile1Written)
        {
                   inFile1 >> mClientNumber;
                   inFile1 >> mClientName;
                   inFile1Written = false;
        }
      if (!inFile2.eof() && !inFile2Written)
        {
                   inFile2 >> fClientNumber;
                   inFile2 >> fClientName;
                   inFile2Written = false;
        }
      if (!inFile1.eof() && !inFile2.eof())
        {
                   atLeastOneFileNotAtEnd = false;
        }
       
     }
    inFile1.close();
    inFile2.close();
    outFile1.close();
    cout << "Merging Complete" << endl;
    system("PAUSE");
    return 0;                                          
}
Thank you for looking into my problem vichu8888. I able able to compile now and when the program runs I only receive the Client numbers and names from the female client in the merged file. Might you have a reason why this would happen? Thank you for your time.
Can you show your input file data and also actual question in your book,plz?
Last edited on
The question reads
" The Cupid Matchmaking Service maintains two files--one for male clients and another for female clients. Each file contains a client ID, last name, first name, and address. Each file is in client ID number order. Design the logic for a program that merges the two files into one file containing a list of all clients, maintaining ID number order."

The address have been left out. Thanks again for your time vichu8888. I do appreciate it.

Female Clients

21 Barbara_Jones
82 Sue_Todd
275 Mary_Donald
276 Cathy_Conner
300 Debbie_Baker
400 Kim_Tanner

Male Clients

56 Bob_Johnson
100 Dan_Smith
200 Mike_Tay
Hi geowalsh3

I have written some code for your program, check it out. It works. But it not same as yours. It is totally different.
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
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
	//declared four ifstream variables because two(i1,i2) used for getting the length of the file 
	//two(in1,in2) for calcualtions purposes
    ifstream i1,i2,in1,in2;
    
    ofstream out;

	//Variable declrations
    int male_id, female_id;
    string male_name, female_name;

    int len1=0;//Initialising male file length
	int len2=0;//initialising female length
    
    cout << "File merge processing starting" << endl;
    
    i1.open("MaleClients.rtf");
	i2.open("FemaleClients.rtf");
	out.open("MergeClients.rtf");
	
	//Calculating male file length
	while(!i1.eof())
	{
		i1>>male_id>>male_name;
		len1++;
	}
	
	//Calcualting female file length
	while(!i2.eof())
	{
		i2>>female_id>>female_name;		
		len2++;
	}
	//Checking the file length by outputting the both lengths
	cout<<"Male file length: " <<len1<<" || Female File length: "<<len2<<endl;

	//opening input files for calculations purposes
	 in1.open("MaleClients.rtf");
	 in2.open("FemaleClients.rtf");


	if(len2>len1)//Condition true when female file length is greater
	{		
		while(!in2.eof())//read until eof of feamle file 
		{					
			in2>>female_id>>female_name;//read first set of input from female data			
			if(!in1.eof())//if male file havent reached end of file,loop executes
			{
				in1>>male_id>>male_name;//read the male data

				//This condition is to check whose ID is greater and print accordingly
				if(male_id<female_id)
				{
					out<<male_id<<" "<<male_name<<endl;
					out<<female_id<<" "<<female_name<<endl;
				}
				else
				{
					out<<female_id<<" "<<female_name<<endl;
					out<<male_id<<" "<<male_name<<endl;
				}
			}

			//If the male input data file finished, it just read female data and write to the file
			else
				out<<female_id<<" "<<female_name<<endl;
		}
	}
	else
		//Here you can write the code when len1>len2 i.e) when male data file have more members than female
		//just copy and paste the above code here and change male data's to feamle data's and vice versa
					
	


	
     i1.close();
    i2.close();
    in1.close();
    in2.close();
    out.close();
    cout << "Merging Complete" << endl;
    system("PAUSE");
    return 0;                                          
}


I have given the comments too, If you dont understand please let me know which part
Thanks for taking the time to look over this. The only thing is my instructor gives us the pseudocode and then we have to write the program from what he gives us. I'm impressed with how fast you wrote this code. Were you able to see what was causing the problem in my code. The only reason I am asking is because I have to turn in the program as he wrote it. I can't thank you enough for all the time you have spent on this with me.
Try this. I followed the pseudocode you have given. Now it works perfect. You got confused with !infile.eof() and infile,eof().
while(!infile.eof(){} - This while loop continues until end of file is reached
while(infile.eof()){}- This while executes only if the end of the file is reached

Apart from that, there were several minor errors with braces for '{}' if else condition. After correcting all those, the program worked.

I didnt see your pseudocode at first. When I read two line of it, I thought it was your try out. So I just skipped it. Otherwise this will be done few hours before itself


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
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
	ifstream inFile1;
	ifstream inFile2;
	ofstream outFile1;
	int mClientNumber, fClientNumber;
	string mClientName, fClientName;
	bool atLeastOneFileNotAtEnd = true;
	bool inFile1Written = false;
	bool inFile2Written = false;

	cout << "File merge processing starting" << endl;

	inFile1.open("male.txt");
	inFile2.open("female.txt");
	outFile1.open("Merge.txt");

	inFile1 >> mClientNumber;
	inFile1 >> mClientName;
	inFile2 >> fClientNumber;
	inFile2 >> fClientName;
	while(atLeastOneFileNotAtEnd == true)
	{ 
		if(inFile1.eof())
		{
			if (inFile2Written == false)
			{
				outFile1 << fClientNumber<<" "<< fClientName << endl;
				inFile2Written = true;
			}
		}
		else if (inFile2.eof())
		{
			if (inFile1Written == false)
			{
				outFile1 << mClientNumber<<" "<< mClientName << endl;
				inFile1Written = true;
			}
		}
		else if (mClientNumber < fClientNumber)
		{
			outFile1 << mClientNumber<<" "<< mClientName << endl;
			inFile1Written = true;
		}
		else
		{
			outFile1 << fClientNumber<<" "<< fClientName << endl;
			inFile2Written = true;
		}


		if (!inFile1.eof() && inFile1Written==true)
		{
			inFile1 >> mClientNumber;
			inFile1 >> mClientName;
			inFile1Written = false;
		}
		if (!inFile2.eof() && inFile2Written==true)
		{
			inFile2 >> fClientNumber;
			inFile2 >> fClientName;
			inFile2Written = false;
		}
		if (inFile1.eof() && inFile2.eof())
		{
			atLeastOneFileNotAtEnd = false;
		}

	}
	inFile1.close();
	inFile2.close();
	outFile1.close();
	cout << "Merging Complete" << endl;
	system("PAUSE");
	return 0;                                          
}
Thanks for all your help. I had a feeling I had other issues. I'm trying to grasp writing C++ one day at a time. Thanks again for helping me out.
Topic archived. No new replies allowed.