3 Replies - 1662 Views - Last Post: 16 April 2007 - 06:47 AM Rate Topic: -----

#1 utkarshs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 07

File Manipulation syntax

Posted 16 April 2007 - 06:12 AM

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

using std :: cin;
using std :: cout;
using std :: endl;
using std :: ifstream;
using std :: ofstream;
using std :: ios;

void loadUnitDetails(float rentArray[][3], int& numUnits);
int getMenuChoice();
void checkUnit(float rentArray[][3], int numUnits);
void makeUnitPayment(float rentArray[][3], int numUnits, int unitNum);
void displayUnitDetails(float rentArray[][3], int numUnits, int unitNum);
void writeAccountSummary(float rentArray[][3], int numUnits);
void saveUnitDetails(float rentArray[][3], int numUnits);

const int CHECK_UNIT = 1;
const int ACCOUNT_SUMMARY = 2;
const int QUIT = 3;
const int MAX_UNITS = 100;

//*************************************************************//
int main (void)
//*************************************************************//
{
   int numberOfUnits = 0;
   float rentArray[MAX_UNITS][3];
	

   loadUnitDetails(rentArray, numberOfUnits);
   
   cout << "Welcome to Rent Accounting \n\n";
   int menuChoice = getMenuChoice();
   while (menuChoice != QUIT)
   {
	   switch (menuChoice)
	   {
		   case CHECK_UNIT : 
				checkUnit(rentArray, numberOfUnits); 
			 break;
	case ACCOUNT_SUMMARY : 
		 writeAccountSummary(rentArray, numberOfUnits); 
			 break;
	   }

	   menuChoice = getMenuChoice();
   }

   saveUnitDetails(rentArray, numberOfUnits);
   cout << "\nProgram Terminated \n\n";
   return 0;
}

// Function "loadUnitDetails" loads the two dimensional rent
// array from the data file "rent.dat".

//-------------------------------------------------------------//
void loadUnitDetails(float rentArray[][3], int& numUnits)
//-------------------------------------------------------------//
{
	ifstream inFile;
	inFile.open( "rent.dat", ios::in );

	if ( inFile.fail() )
	{
		cout << "Unable to open rent.dat";
	 	cout << endl;
		return 1;
	}	

	while( !inFile.eof() )
	{
		for( int i=0; i<MAX_UNITS; i++){
 	 		inFile >> rentArray[i][0];
			inFile >> rentArray[i][1];
			inFile >> rentArray[i][2];
		}
	}
	
	inFile.close();

	return 0;
}

//  Function "getMenuChoice" get a menu choice.

//-------------------------------------------------------------//
int getMenuChoice()
//-------------------------------------------------------------//
{
   int choice = 0;
   cout << "\n";
   cout << "Main Menu\n\n";
   cout << "1. Check account for a unit \n\n";
   cout << "2. Print account summary for all units\n\n";
   cout << "3. Quit program \n\n";
   cout << "Enter menu choice <1, 2, or 3> : ";
   cin >> choice;
   cout << "\n";
   return choice;
}

// Function "displayUnitDetails" displays the rent details owing
// on a unit.

//--------------------------------------------------------------------//
void displayUnitDetails(float rentArray[][3], int numUnits, int unitNum)
//--------------------------------------------------------------------//
{
	cout << "Unit number : " << unitNum; 
	cout << endl << "Current amount owing is ";
	cout << rentArray[unitNum][0];
	cout << endl << "One week overdue owing is ";
	cout << rentArray[unitNum][1];
	cout << endl << "2 or more weeks overdue is " 
	cout << rentArray[unitNum][2];

}

// Function “makeUnitPayment” allows a payment to be made on a 
// particular unit.

//-----------------------------------------------------------------//
void makeUnitPayment(float rentArray[][3], int numUnits, int unitNum)
//-----------------------------------------------------------------//
{
   int payment = 0;
   cout << endl << endl;
   cout << "\n\nEnter payment amount ==> ";
   cin >> payment;
   cout << "\n\n";
	if(rentArray[unitNum][2]!=0){
		if((rentArray[unitNum][2]-payment)<0){
			rentArray[unitNum][2]=0;
			payment=payment-rentArray[unitNum][2];
		}
		if((rentArray[unitNum][2]-payment)==0){
			rentArray[unitNum][2]=0;
			payment=0;
		}	
		if((rentArray[unitNum][2]-payment)>0){
		  rentArray[unitNum][2]=rentArray[unitNum][2]- payment;
		  payment=0;	
		}
	else if((payment!=0)&&(rentArray[unitNum][1]!=0)){
		if((rentArray[unitNum][1]-payment)<0){
			rentArray[unitNum][1]=0;
			payment=payment-rentArray[unitNum][2];
		}
		if((rentArray[unitNum][1]-payment)==0){
			rentArray[unitNum][1]=0;
			payment=0;
		}	
		if((rentArray[unitNum][1]-payment)>0){
		  rentArray[unitNum][1]=rentArray[unitNum][1]- payment;
		  payment=0;	
		}
	}
	else if((payment!=0)&&(rentArray[unitNum][0]!=0)){
		if((rentArray[unitNum][0]-payment)>0){
		  rentArray[unitNum][0]=rentArray[unitNum][0]- payment;
		  payment=0;	
		}	
		else {
		  rentArray[unitNum][0]=0;
		  payment=0;
		}
	}
	else {
		rentArray[unitNum][0]=0;
		  payment=0;
		}
}

// Function "checkUnit" is used the display the current amount
// owed on a particular unit and also allow payments to be made 
// on outstanding rent owed.

//-------------------------------------------------------------//
void checkUnit(float rentArray[][3], int numUnits)
//-------------------------------------------------------------//
{
   int unitNum = 0;
   cout << "\n";
   cout << "Enter unit number: ";
   cin >> unitNum;
   cout << "\n\n";
   displayUnitDetails(rentArray, numUnits, unitNum);
   makeUnitPayment(rentArray, numUnits, unitNum);
}

// Function "displayAccountSummary" displays the total amount
// owed by all units, total amount owed currently, amount in 
// arrears of greater than 1 week, and greater than 2 weeks, 
// and a listing of all units in arrears.

//-------------------------------------------------------------//
void writeAccountSummary(float rentArray[][3], int MAX_UNITS)
//-------------------------------------------------------------//
{
   cout << "\n";
   cout << "Accounting Summary of all units \n\n";
	for(i=0;i<=Max_UNITS;i++){
		displayUnitDetails(rentArray, MAX_UNITS, i);
	}
}

// Function "saveUnitDetails" saves the contents of the two 
// dimensional array containing the unit details to the file 
// "rent.dat".

//-------------------------------------------------------------//
void saveUnitDetails(float rentArray[][3], int MAX_UNITS)
//-------------------------------------------------------------//
{
   ofstream outFile;
   outFile.open( "rent.dat", ios::out );
	for (i=0;i<MAX_UNITS; i++){
		outfile << rentArray[i][0];
		outfile <<"  ";
		outfile << rentArray[i][1];
		outfile <<"  ";
		outfile << rentArray[i][2];
		outfile <<"  ";
		otufile <<endl;
	}
  
	if(outFile.fail()){
		cout << "Error opening";
		cout << endl;
		return false;
	}		

	outfile.close();
	return true;
}


I have no idea if my file input and output are correct syntax. The program takes an input in the form of array from the file. I need some help urgently on this.

Is This A Good Question/Topic? 0
  • +

Replies To: File Manipulation syntax

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: File Manipulation syntax

Posted 16 April 2007 - 06:15 AM

Are you getting any errors? If so, please post them. Is the program eliciting undesired behaviour? If so, can you describe?
Was This Post Helpful? 0
  • +
  • -

#3 utkarshs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 07

Re: File Manipulation syntax

Posted 16 April 2007 - 06:40 AM

View PostAmadeus, on 16 Apr, 2007 - 06:15 AM, said:

Are you getting any errors? If so, please post them. Is the program eliciting undesired behaviour? If so, can you describe?


there are errors which ill fix up I just needed to know the syntax for inputting from a file rent.dat
the file is 30 40 50
60 70 80
90 100 110
into an array
rentarray = { 30 , 40 ,50
60 , 70 , 80
90 , 100, 110 }
thatz all i need other than that i think i can fix the rest of it up.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: File Manipulation syntax

Posted 16 April 2007 - 06:47 AM

int i=0;
while((infile.eof())
{
   infile>>rentarray[i][0]>>rentarray[i][1]>>rentarray[i][2];
   i++;
}


Something like that should work fine...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1