Else if Statement syntax correction.

Hello everyone, i have a short assignment to do for my Introduction To Program Class. I've been working on it over and over trying to figure out what is wrong but cannot seem to find it. It is supposed to take two files, a master file and transaction file, and make a new master file with it. I keep getting spammed with the error codes we're tasked to program with it, and my professor told me my else if statements were incorrect but wont help me besides that. I uploaded my code here,

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
#include <iostream>
#include <fstream>
using namespace std;
int main () 
{
	//Declarations
	ifstream masterFile;
	ifstream transactionFile;
	ofstream newMasterFile;
	int mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
	string mClientfName, mClientlName;
	
	//Opening files
	cout << "Now starting Master file Update";
	masterFile.open("Master.rtf");
	transactionFile.open("Transaction.rtf");
	newMasterFile.open("newMaster.rtf");
	masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
	transactionFile >> tClientNumber >> titemClientCost;
	
	//Main loop structure
	while (!transactionFile.eof())
		{
		while ((!masterFile.eof()) && (mClientNumber < tClientNumber))
			{
			newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
			masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
			}
		if (masterFile.eof()) 
			{
			cout << "Error Client ID: " << tClientNumber << " not in Master File." << endl;
			}
			else if (mClientNumber == tClientNumber)
				{
				mtotalClientCost = mtotalClientCost + titemClientCost;
				newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
				masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
				}
			else if (mClientNumber > tClientNumber) 
			{
				cout << "Error Client ID: " << tClientNumber << " not in Master File." << endl;
			}
			transactionFile >> tClientNumber >> titemClientCost;
		} 
		while (!masterFile.eof())
		{
			newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
			masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
		}
		masterFile.close();
		transactionFile.close();
		newMasterFile.close();
		return 0;
}


It's supposed to take info from Transactions.rtf and Master.rtf and create a new combined file of newMaster.rtf
Could someone please help me correct this?

Last edited on
stream.eof() should be avoided.

Your indentation is super misleading from lines 23 to 53. Make sure your curly braces always line up, and you indent once per each loop/while.

How does your master file correspond with your transaction file? Are they somehow synced, data-wise? Is it in some way ordered? I don't know what your exact prompt is, so I'm just going to give some tips. It looks like you're making this more complicated than it needs to be.

It looks like the pattern of your master file is "mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost" each time. I'm less clear on what your transaction file is, but is that also a pattern of "client_number titem_client_cost client_number _titem_client_cost .. etc"?

You should loop on the data you are trying to extract so you immediately know when you are successfully extracting. Looping on .eof() and then trying to get the data after you check if the file is good is bug-prone.

This is just a guess, but something like...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost
    && transactionFile >> tClientNumber >> titemClientCost)
{
	std::cout << mClientNumber << " " << tClientNumber << " " << titemClientCost;

	if (mClientNumber == tClientNumber)
	{
		mtotalClientCost = mtotalClientCost + titemClientCost;
	}
	else if (mClientNumber > tClientNumber) 
	{
		cout << "Error Client ID: " << tClientNumber << " not in Master File." << endl;
	}

	newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
}

Your indentation is off. I've reposted your code with the indentation corrected.

Also, fstreams aren't files. They're streams backed by files and they're C++ objects. This means you don't open/close them manually. They also have state { good, bad, failed }. So you test for that, and not End Of File.
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
#include <iostream>
#include <fstream>
#include <string>	// kbw: you used std::string, so you need this

using namespace std;

int main() 
{
	//Declarations
	int mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
	string mClientfName, mClientlName;

	//Opening files
	cout << "Now starting Master file Update";
	ifstream masterFile("Master.rtf");
	ifstream transactionFile("Transaction.rtf");
	ofstream newMasterFile("newMaster.rtf");

	masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
	transactionFile >> tClientNumber >> titemClientCost;

	//Main loop structure
	while (!transactionFile)
	{
		while ((!masterFile) && (mClientNumber < tClientNumber))
		{
			newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
			masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
		}

		if (masterFile) 
		{
			cout << "Error Client ID: " << tClientNumber << " not in Master File." << endl;
		}
		else if (mClientNumber == tClientNumber)
		{
			mtotalClientCost = mtotalClientCost + titemClientCost;
			newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
			masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
		}
		else if (mClientNumber > tClientNumber) 
		{
			cout << "Error Client ID: " << tClientNumber << " not in Master File." << endl;
		}

		transactionFile >> tClientNumber >> titemClientCost;
	} 

	while (!masterFile)
	{
		newMasterFile << mClientNumber << mClientfName << mClientlName << mtotalClientCost;
		masterFile >> mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost;
	}

	return 0;
}
@Ganado
stream.eof() should be avoided.

Your indentation is super misleading from lines 23 to 53. Make sure your curly braces always line up, and you indent once per each loop/while.

How does your master file correspond with your transaction file? Are they somehow synced, data-wise? Is it in some way ordered? I don't know what your exact prompt is, so I'm just going to give some tips. It looks like you're making this more complicated than it needs to be.

It looks like the pattern of your master file is "mClientNumber >> mClientfName >> mClientlName >> mtotalClientCost" each time. I'm less clear on what your transaction file is, but is that also a pattern of "client_number titem_client_cost client_number _titem_client_cost .. etc"?

You should loop on the data you are trying to extract so you immediately know when you are successfully extracting. Looping on .eof() and then trying to get the data after you check if the file is good is bug-prone.


Sorry, I should've touched more on the contents of the files and the actual prompt. our prompt is pretending that a company maintains a master file that contains a record for each of its clients. Fields in the master file include the client's ID number, first name, last name, and total amount spent this year. Every week, a transaction file is produced. It contains a customer's ID number, the service recieved, and the price paid. Each file is sorted in ID number order. We're supposed to design the logic for a program that matches the master and current week's price paid to the cumulative total. The output is the updated master file and an error report that lists any transaction records for which no record exists. Thank you for your response and helping me fix your indentation. The pattern and structure of the looping is redundant, but it for the assignment we are provided with psuedocode, and required to turn it into functional C++.
The psuedocode provided is below.

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
// Pseudocode PLD Chapter 7 #6a pg. 301
// Start
//     Declarations
//         InputFile masterFile;
//         InputFile transactionFile;
//         OutputFile newMasterFile;
//         num mClientNumber, mtotalClientCost, tClientNumber, titemClientCost
//         string mClientfName, mClientlName 
//     output "Master File Updating Starting"
//     open masterFile "Master.rtf"
//     open transactionFile "Transaction.rtf"
//     open newMasterFile "newMaster.rtf"
//     read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//     read tClientNumber, titemClientCost from transactionFile
//     while ( transactionFile not EOF )
//         while (( masterFile not EOF) and (mClientNumber < tClientNumber))
//             output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//             read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//         endwhile
//         if (masterFile is EOF)
//             output "Error Client ID: ", tClientNumber, " not in Master File."
//         else if (mClientNumber == tClientNumber) then
//             mtotalClientCost = mtotalClientCost + titemClientCost
//             output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//             read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//         else if (mClientNumber > tClientNumber) then
//             output "Error Client ID: ", tClientNumber, " not in Master File."
//         endif
//         read tClientNumber, titemClientCost from transactionFile
//     endwhile
//     while (masterFile not EOF)
//         output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//         read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//     endwhile
//     output "Master File Updating Complete"
//     close masterFile
//     close transactionFile
//     close newMasterFile
// Stop 
Last edited on
@kbw
Your indentation is off. I've reposted your code with the indentation corrected.

Also, fstreams aren't files. They're streams backed by files and they're C++ objects. This means you don't open/close them manually. They also have state { good, bad, failed }. So you test for that, and not End Of File.


Hello, thank you for correcting my indentation and fstream misuse, I'm not sure how to test for their state, all i know is our assignment we're supposed to follow the psuedocode provided, and test for it to be the eof of the master and transaction. I apologize, i should've put more information about the assignment and about the prompt. Here is the provided psuedocode.

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
// Pseudocode PLD Chapter 7 #6a pg. 301
// Start
//     Declarations
//         InputFile masterFile;
//         InputFile transactionFile;
//         OutputFile newMasterFile;
//         num mClientNumber, mtotalClientCost, tClientNumber, titemClientCost
//         string mClientfName, mClientlName 
//     output "Master File Updating Starting"
//     open masterFile "Master.rtf"
//     open transactionFile "Transaction.rtf"
//     open newMasterFile "newMaster.rtf"
//     read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//     read tClientNumber, titemClientCost from transactionFile
//     while ( transactionFile not EOF )
//         while (( masterFile not EOF) and (mClientNumber < tClientNumber))
//             output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//             read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//         endwhile
//         if (masterFile is EOF)
//             output "Error Client ID: ", tClientNumber, " not in Master File."
//         else if (mClientNumber == tClientNumber) then
//             mtotalClientCost = mtotalClientCost + titemClientCost
//             output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//             read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//         else if (mClientNumber > tClientNumber) then
//             output "Error Client ID: ", tClientNumber, " not in Master File."
//         endif
//         read tClientNumber, titemClientCost from transactionFile
//     endwhile
//     while (masterFile not EOF)
//         output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
//         read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
//     endwhile
//     output "Master File Updating Complete"
//     close masterFile
//     close transactionFile
//     close newMasterFile
// Stop  
Topic archived. No new replies allowed.