Tuesday, July 21, 2009

Monthly Expenses – Read from File

/*Program to read from Ex.03 previous post*/

/*Read single record only*/

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fileIn;

struct income {

float nSalary; //Salary Received

float nRentRec; //Rent Received from home at native place

float nOtherIn; //Any other income

};

struct expenses {

float nRentPaid; //Rent

float nPUB; //Public Utility Bill (water, electricity)

float nTele; //Telephone, mobile bills

float nEdKid; //Education for kids

float nEdSelf; //Education for self

float nMed; //Medical expenses

float nInsur; //Insurance premium payable

};

struct income MyIncome;

struct expenses MyExpenses;

float nInTotal, nExTotal, nBalance;

fileIn = fopen("C:\\ex03.txt","r+");

clrscr();

fscanf(fileIn,

     "%f %f %f %f %f %f %f %f %f %f",

     &MyIncome.nSalary, &MyIncome.nRentRec, &MyIncome.nOtherIn,

     &MyExpenses.nRentPaid, &MyExpenses.nPUB, &MyExpenses.nTele,

     &MyExpenses.nEdKid, &MyExpenses.nEdSelf, &MyExpenses.nMed,

     &MyExpenses.nInsur);

cout<<"Monthly Expenses Vs Income\n";

cout<<"--------------------------\n";

cout<<"INCOME\n";

cout<<"======\n";

cout<<"\nSalary :"<<MyIncome.nSalary;

cout<<"\nRent Received :"<<MyIncome.nRentRec;

cout<<"\nOther Income :"<<MyIncome.nOtherIn;

cout<<"\nEXPENSES\n";

cout<<"========\n";

cout<<"\nRent Paid :"<<MyExpenses.nRentPaid;

cout<<"\nPublic Utility :"<<MyExpenses.nPUB;

cout<<"\nTelephone :"<<MyExpenses.nTele;

cout<<"\nEducation-Kids :"<<MyExpenses.nEdKid;

cout<<"\nEducation-Self :"<<MyExpenses.nEdSelf;

cout<<"\nMedical :"<<MyExpenses.nMed;

cout<<"\nInsurance :"<<MyExpenses.nInsur;

nInTotal = MyIncome.nSalary + MyIncome.nRentRec + MyIncome.nOtherIn;

nExTotal = MyExpenses.nRentPaid + MyExpenses.nPUB

     + MyExpenses.nTele + MyExpenses.nEdKid

     + MyExpenses.nEdSelf + MyExpenses.nMed

     + MyExpenses.nInsur;

nBalance = nInTotal - nExTotal;

cout<<"\nTotal Income : "<<nInTotal;

cout<<"\nTotal Expenses : "<<nExTotal;

if (nBalance<0)

cout<<"\nDeficit : ";

else

cout<<"\nSurplus : ";

cout<<nBalance;

fclose(fileIn);

getch();

}

 

/*Program to read from Ex.03 previous post*/

/*Read single record only*/

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fileIn;

struct income {

float nSalary; //Salary Received

float nRentRec; //Rent Received from home at native place

float nOtherIn; //Any other income

};

struct expenses {

float nRentPaid; //Rent

float nPUB; //Public Utility Bill (water, electricity)

float nTele; //Telephone, mobile bills

float nEdKid; //Education for kids

float nEdSelf; //Education for self

float nMed; //Medical expenses

float nInsur; //Insurance premium payable

};

struct income MyIncome;

struct expenses MyExpenses;

float nInTotal, nExTotal, nBalance;

fileIn = fopen("C:\\ex03.txt","r+");

clrscr();

while (! feof(fileIn))

{

fscanf(fileIn,

     "%f %f %f %f %f %f %f %f %f %f",

     &MyIncome.nSalary, &MyIncome.nRentRec, &MyIncome.nOtherIn,

     &MyExpenses.nRentPaid, &MyExpenses.nPUB, &MyExpenses.nTele,

     &MyExpenses.nEdKid, &MyExpenses.nEdSelf, &MyExpenses.nMed,

     &MyExpenses.nInsur);

cout<<"\n\nMonthly Expenses Vs Income\n";

cout<<"--------------------------\n";

cout<<"INCOME\n";

cout<<"======\n";

cout<<

cout<<"\nSalary :"<<MyIncome.nSalary;

cout<<"\nRent Received :"<<MyIncome.nRentRec;

cout<<"\nOther Income :"<<MyIncome.nOtherIn;

cout<<"\nEXPENSES\n";

cout<<"========\n";

cout<<"\nRent Paid :"<<MyExpenses.nRentPaid;

cout<<"\nPublic Utility :"<<MyExpenses.nPUB;

cout<<"\nTelephone :"<<MyExpenses.nTele;

cout<<"\nEducation-Kids :"<<MyExpenses.nEdKid;

cout<<"\nEducation-Self :"<<MyExpenses.nEdSelf;

cout<<"\nMedical :"<<MyExpenses.nMed;

cout<<"\nInsurance :"<<MyExpenses.nInsur;

nInTotal = MyIncome.nSalary + MyIncome.nRentRec + MyIncome.nOtherIn;

nExTotal = MyExpenses.nRentPaid + MyExpenses.nPUB

     + MyExpenses.nTele + MyExpenses.nEdKid

     + MyExpenses.nEdSelf + MyExpenses.nMed

     + MyExpenses.nInsur;

nBalance = nInTotal - nExTotal;

cout<<"\nTotal Income : "<<nInTotal;

cout<<"\nTotal Expenses : "<<nExTotal;

if (nBalance<0)

cout<<"\nDeficit : ";

else

cout<<"\nSurplus : ";

cout<<nBalance;

};

fclose(fileIn);

getch();

}

No comments:

Post a Comment