Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,815 people online right now. Registration is fast and FREE... Join Now!




File Manipulation syntax

 
Reply to this topicStart new topic

File Manipulation syntax, c++ file input

utkarshs
16 Apr, 2007 - 05:12 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 2


My Contributions
CODE
#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.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: File Manipulation Syntax
16 Apr, 2007 - 05:15 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Are you getting any errors? If so, please post them. Is the program eliciting undesired behaviour? If so, can you describe?
User is offlineProfile CardPM
+Quote Post

utkarshs
RE: File Manipulation Syntax
16 Apr, 2007 - 05:40 AM
Post #3

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 2


My Contributions
QUOTE(Amadeus @ 16 Apr, 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?


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.


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: File Manipulation Syntax
16 Apr, 2007 - 05:47 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
CODE

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

Something like that should work fine...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month