Hello everyone,

My name is David and I am new to these forums as far as posting goes, but I have used alot of the advice given here in my programs. I have come across a problem though in my program that I cant seem to figure out why it is doing what it is doing!

What I am trying to do on this particular function is read through the structures I have listed in the fstream object, and when a part of the structure( .deleted) is false, it reads the (.agent fee and .purchase price) multiplies those together, and then after it loops through the entire file and finds all of the structures that are not deleted, gives a total of the fees divided by the number of files that were read that were not .deleted = true.

Sorry if this is confusing, and if I am doing this wrong, I am sorry, and please just ignore me!

Here is what I am having trouble with though, I have posted the code below. The loop I have seems to not read the .eof() and just continues to loop forever, instead of stopping the loop.

PROBLEM AREA: where I have the cout << I am here3, that is the part that continues to loop forever and never stops.... any help would be greatly appreciated.

double SummaryOfFees(){

	int numberOfClients = 0;
	double feeTotal = 0;
	double feeTemp;
	double summaryOfFee;
	ClientInformation RecordToRead;

	fstream realEstate("RealEstateList.txt", ios::in|ios::binary);

	realEstate.read(reinterpret_cast<char *>(&RecordToRead), sizeof(ClientInformation));

	while(!realEstate.eof()){
		cout << "I am here!3" << endl; //just used for debugging

		if(!RecordToRead.deleted){ //if file is not deleted, procede

			numberOfClients++;
			feeTemp =(RecordToRead.agentFee * RecordToRead.purchasePrice);
			feeTotal = feeTotal + feeTemp;
		}
	}
	realEstate.close();
	summaryOfFee = (feeTotal / numberOfClients);
	return summaryOfFee;

}

FULL CODE:

/* David Tolley
** CS1410
** Assignment #3
** RealEstateAgent
** A-Number: A01098213
** Email: David.Tolley@aggiemail.usu.edu
*/

#include<fstream>
#include<iostream>
#include<iomanip>

using namespace std;
//constants
	const int SIZE_NAME = 20;
	const int SIZE_DATE = 20;

//structure
	struct ClientInformation{
		char lastName[SIZE_NAME];
		char firstName[SIZE_NAME];
		char closingDate[SIZE_DATE];
		double purchasePrice;
		double loanAmount;
		double agentFee;
		bool deleted;
	};

//function prototypes
	void DisplayAllClients();

	double SummaryOfFees();

	void AddNewClient();

	void ModifyExistingClient();

	bool RemoveClient();

	void PrintMenu();

	void InitFile();
	

int main(){

//stores user option
	int menuSelection;
	double summaryOfFees;

	do{

		PrintMenu();

		cin >> menuSelection;

		if(menuSelection < 1 || menuSelection > 6)
		{
			cout << endl << "Please enter a valid selection (1-6)." << endl << endl;
		}

		InitFile();

		switch(menuSelection){

			case 1:{
					//displays all the clients
					DisplayAllClients();
				   }
				break;

			case 2:{
					//summary of fees
					summaryOfFees = SummaryOfFees();
					cout << "Summary Of Fees: $" << summaryOfFees << endl;
				   }
				break;

			case 3:{
					//adds new clients
					AddNewClient();
				   }
				break;

			case 4:{
					//modifys an existing client
					ModifyExistingClient();
				   }
				break;

			case 5:{
					//removes a client
					if(RemoveClient())
						cout << "Record has been deleted." << endl;

					else
						cout << "Sorry, the record you entered does not exist." << endl;
				   }
				break;

			case 6:
				//exits program
				return 0;
		}

	}while(menuSelection != 6);

}

//modifys an existing client
void ModifyExistingClient(){

	ClientInformation RecordToModify;
	char lastName[SIZE_NAME];

	fstream realEstate("RealEstateList.txt", ios::in|ios::out|ios::binary);

	cout << "Enter the Last Name of the file to modify: ";
	cin.ignore();
	cin.getline(lastName, SIZE_NAME);

	realEstate.read(reinterpret_cast<char *>(&RecordToModify), sizeof(ClientInformation));
	int count = 0;

	while(!realEstate.eof()){

		if(strcmp(lastName, RecordToModify.lastName) == 0){

		
	}

}

//displays a summary of fees
double SummaryOfFees(){

	int numberOfClients = 0;
	double feeTotal = 0;
	double feeTemp;
	double summaryOfFee;
	ClientInformation RecordToRead;

	fstream realEstate("RealEstateList.txt", ios::in|ios::binary);

	realEstate.read(reinterpret_cast<char *>(&RecordToRead), sizeof(ClientInformation));

	while(!realEstate.eof()){
		cout << "I am here!3" << endl; //just used for debugging

		if(!RecordToRead.deleted){ //if file is not deleted, procede

			numberOfClients++;
			feeTemp =(RecordToRead.agentFee * RecordToRead.purchasePrice);
			feeTotal = feeTotal + feeTemp;
		}
	}
	realEstate.close();
	summaryOfFee = (feeTotal / numberOfClients);
	return summaryOfFee;

}
//removes a client
bool RemoveClient(){

char firstName[SIZE_NAME];
char lastNameEntered[SIZE_NAME];
int yesOrNo;
ClientInformation RecordToDelete;

fstream realEstate("RealEstateList.txt", ios::in|ios::out|ios::binary);

cout << "Enter the Last Name of the client you want to remove: ";
cin.ignore();
cin.getline(lastNameEntered, SIZE_NAME);

realEstate.read(reinterpret_cast<char *>(&RecordToDelete), sizeof(ClientInformation));

int count = 0;

while(!realEstate.eof()){

	if(strcmp(lastNameEntered, RecordToDelete.lastName) == 0 && RecordToDelete.deleted == false){

		cout << "Is this the client you wish to delete?" << endl;
		cout << "Name: " << RecordToDelete.firstName << " " << RecordToDelete.lastName << endl;
		cout << "Closing Date: " << RecordToDelete.closingDate << endl;
		cout << "Purchase Price: " << RecordToDelete.purchasePrice << endl;
		cout << "Loan Amount: " << RecordToDelete.loanAmount << endl;
		cout << "Agent Fee %: " << RecordToDelete.agentFee << endl;

		cout << "Enter 1 for YES or 2 for NO: ";

		cin >> yesOrNo;

			if(yesOrNo == 1)

			{
				realEstate.seekg(count*sizeof(ClientInformation), ios::beg);
				realEstate.read(reinterpret_cast<char *>(&RecordToDelete),sizeof(ClientInformation));
				RecordToDelete.deleted = true;
				realEstate.seekp(count*sizeof(ClientInformation),ios::beg);
				realEstate.write(reinterpret_cast<char *>(&RecordToDelete),sizeof(ClientInformation));
				realEstate.close();
				return true;
			}
	}

	realEstate.read(reinterpret_cast<char *>(&RecordToDelete), sizeof(ClientInformation));
	count++;

}
realEstate.clear();
realEstate.close();
return false;

}

//function to add a new client
void AddNewClient(){
	ClientInformation PersonToAdd;
	char lastName[SIZE_NAME];
	char firstName[SIZE_NAME];
	char closingDate[SIZE_DATE];
	double purchasePrice;
	double loanAmount;
	double agentFee;

	fstream realEstate("RealEstateList.txt", ios::app|ios::binary);
	if(!realEstate){
		cout << "Error opening RealEstateList.txt." << endl << "Please try again." << endl;
	}

	cout << "Enter the Last Name of the client to add: ";
	cin.ignore();
	cin.getline(lastName, SIZE_NAME);

	cout << "Enter the First Name of the client to add: ";
	cin.getline(firstName, SIZE_NAME);

	cout << "Enter the Closing Date - Month Day, Year(January 21, 2008): ";
	cin.getline(closingDate, SIZE_DATE); 

	cout << "Enter the Purchase Price: ";
	cin >> purchasePrice;

	cout << "Enter the Loan Amount: ";
	cin.ignore();
	cin >> loanAmount;

	cout << "Enter the Agent Fee %: ";
	cin.ignore();
	cin >> agentFee;

	//copys enterd data to structure
	strcpy_s(PersonToAdd.lastName, SIZE_NAME, lastName);
	strcpy_s(PersonToAdd.firstName, SIZE_NAME, firstName);
	strcpy_s(PersonToAdd.closingDate, SIZE_DATE, closingDate);
	PersonToAdd.purchasePrice = purchasePrice;
	PersonToAdd.loanAmount = loanAmount;
	PersonToAdd.agentFee = agentFee;
	PersonToAdd.deleted = false;

	//writes structure to file
	realEstate.write(reinterpret_cast<char *>(&PersonToAdd), sizeof(ClientInformation));

realEstate.clear();
realEstate.close();
}
//function to display clients
void DisplayAllClients(){
	ClientInformation Record;
	fstream realEstate("RealEstateList.txt", ios::in|ios::binary);

	if(!realEstate){
		cout << "Error opening RealEstateList.txt." << endl << "Please try again." << endl;
	}

	realEstate.read(reinterpret_cast<char *>(&Record), sizeof(ClientInformation));

	while(!realEstate.eof()){

		if(!Record.deleted){

			cout << "\nName: " << Record.firstName << " " << Record.lastName << endl;
			cout << "Closing Date: " << Record.closingDate << endl;
			cout << "Purchase Price: " << Record.purchasePrice << endl;
			cout << "Loan Amount: " << Record.loanAmount << endl;
			cout << "Agent Fee %: " << Record.agentFee << endl << endl;
		}
		
		realEstate.read(reinterpret_cast<char *>(&Record),sizeof(ClientInformation));

	}
	realEstate.clear();
	realEstate.close();
}

//initis file if not created
void InitFile(){
	fstream realEstate("RealEstateList.txt", ios::in|ios::binary);

	if(!realEstate){
		fstream realEstate("RealEstateList.txt", ios::out|ios::binary);
		realEstate.close();
	}

	else
		realEstate.close();
}

//prints the user menu
void PrintMenu(){

	cout << "\nDisplay All Clients - 1" <<endl;
	cout << "Summary Of Fees - 2" << endl;
	cout << "Add A New Client - 3"<< endl;
	cout << "Modify An Existing Client - 4"<< endl;
	cout << "Remove A Client - 5"<< endl;
	cout << "Exit Program - 6"<< endl;
	cout << "Enter Your Selection: ";
}

Well when you are inside this loop,

while(!realEstate.eof()){
		cout << "I am here!3" << endl; //just used for debugging

		if(!RecordToRead.deleted){ //if file is not deleted, procede

			numberOfClients++;
			feeTemp =(RecordToRead.agentFee * RecordToRead.purchasePrice);
			feeTotal = feeTotal + feeTemp;
		}
	}

you are not ever reading anything from the fstream "realEstate". The only way that you can hit the end of the file and thus get a true value for the eof function is to read something from the file. Since that doesn't happen inside your loop, the eof function will always return false, so !realEstate.eof () will always be true, so you get an infinite loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.