0

I'm new to C++ and I'm working on a program but on line 39 it says error 'else' without a previous 'if' but I do have a previous if. Does anyone have any input on what I am doing wrong? Heres my code for the program. Thanks.

1 #include <fstream>
2 #include <iostream>
3 #include <cstdlib>
4 using namespace std;
5 int main()
6 { 
7 //Declarations
8     ifstream masterFile;
9     ifstream transactionFile;
10    ofstream newMasterFile;
11    double mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
12    string mClientfName, mClientlName;
13 cout << "Master File Updating Starting" ;
14 masterFile.open("Master.rtf");
15 transactionFile.open("Transaction.rtf");
16 newMasterFile.open("newMaster.rtf");
17 masterFile >> mClientNumber;
18 masterFile >> mClientfName;
19 masterFile >> mClientlName;
20 masterFile >> mtotalClientCost;
21 transactionFile >> tClientNumber;
22 transactionFile >>titemClientCost;
23 while ( transactionFile.eof() )
24 {
25     while (( masterFile.eof()) && (mClientNumber < tClientNumber))
26     {
27         newMasterFile << mClientNumber << endl;
28         newMasterFile << mClientfName << endl;
29         newMasterFile << mClientlName << endl;
30         newMasterFile << mtotalClientCost << endl;
31         masterFile >> mClientNumber;
32         masterFile >> mClientfName;
33         masterFile >> mClientlName;
34         masterFile >> mtotalClientCost;
35     }
36     if (masterFile.eof());
37    {
38         cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
39     else if (mClientNumber == tClientNumber);
40         mtotalClientCost = mtotalClientCost + titemClientCost;
41         newMasterFile << mClientNumber << endl;
42         newMasterFile << mClientfName << endl;
43         newMasterFile << mClientlName << endl;
44         newMasterFile << mtotalClientCost << endl;
45         masterFile >> mClientNumber;
46         masterFile >> mClientfName;
47         masterFile >> mClientlName;
48         masterFile >> mtotalClientCost;
49     else if (mClientNumber > tClientNumber);
50         cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
51     }
52     transactionFile >> tClientNumber;
53     transactionFile >> titemClientCost;
54 }
55 while (masterFile.eof())
56 {
57         newMasterFile << mClientNumber << endl;
58         newMasterFile << mClientfName << endl;
59         newMasterFile << mClientlName << endl;
60         newMasterFile << mtotalClientCost << endl;
61         masterFile >> mClientNumber;
62         masterFile >> mClientfName;
63         masterFile >> mClientlName;
64         masterFile >> mtotalClientCost;
65 }
66 cout << "Master File Updating Complete" ;
67
68 masterFile.close();
69 transactionFile.close();
70 newMasterFile.close();
71
72   system("pause");
73   return 0;
74 }
  • 1
    Not that this was your question, but you may find this interesting http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Cramer Mar 21 '14 at 00:56

4 Answers4

1
if (masterFile.eof());
                  //^^this ; here effectively terminates your if block and 
                 //does not have a closing } before else

similar problem can be found here:

if (mClientNumber == tClientNumber);
                                //^^another line below same problem
taocp
  • 22,020
  • 6
  • 46
  • 60
1

In all of your if statements, you are adding an unnecessary ;.

if (masterFile.eof());{
//                   ^ this semicolon is not supposed to be there

This ; will terminate the if statement.

Instead do this:

if (masterFile.eof()){

Also, you are not closing off the first bracket in the if statement. So the final product should look like:

if (masterFile.eof()){
    //if body
}else if (*next condition*){
    //else if body
}

and so on.. Cheers

ylun.ca
  • 2,352
  • 7
  • 23
  • 47
0

First of all you placed a semicolon after the if and else if statements

if (masterFile.eof());

else if (mClientNumber == tClientNumber);

Rempve them.

If do not take into account this typo yjen the else if has no preceding if statement. Instead it is put into compound statement of the enclosing if statement

if (masterFile.eof());
{
     cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
     else if (mClientNumber == tClientNumber);

Maybe you meant the following

if (masterFile.eof());
{
     cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
}
else if (mClientNumber == tClientNumber);

Or

if (masterFile.eof());
{
     cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
     if (mClientNumber == tClientNumber);
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
0

You forgot { and } in few places. Change lines 36-51 to

36    if (masterFile.eof())
37    {
38         cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
39    } else if (mClientNumber == tClientNumber){
40         mtotalClientCost = mtotalClientCost + titemClientCost;
41         newMasterFile << mClientNumber << endl;
42         newMasterFile << mClientfName << endl;
43         newMasterFile << mClientlName << endl;
44         newMasterFile << mtotalClientCost << endl;
45         masterFile >> mClientNumber;
46         masterFile >> mClientfName;
47         masterFile >> mClientlName;
48         masterFile >> mtotalClientCost;
49     } else if (mClientNumber > tClientNumber){
50         cout << "Error Client ID: " << tClientNumber << " not in Master File." ;
51     }
Avt
  • 16,037
  • 4
  • 48
  • 67
  • You forgot to mention that he/she was placing `;` in wrong spots and terminating the if-statements. – ylun.ca Mar 21 '14 at 00:56