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();

}

Monthly expenses program using structure and file

/* Program using Structure and output to file*/

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fileOut;

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;

int i;

char cOption;

fileOut = fopen("C:\\ex03.txt","w+");

clrscr();

cOption='Y';

do

{

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

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

cout<<"INCOME\n";

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

cout<<"Salary :";cin>>MyIncome.nSalary;

cout<<"Rent Received :";cin>>MyIncome.nRentRec;

cout<<"Other Income :";cin>>MyIncome.nOtherIn;

cout<<"EXPENSES\n\n";

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

cout<<"Rent Paid :";cin>>MyExpenses.nRentPaid;

cout<<"Public Utility :";cin>>MyExpenses.nPUB;

cout<<"Telephone :";cin>>MyExpenses.nTele;

cout<<"Education-Kids :";cin>>MyExpenses.nEdKid;

cout<<"Education-Self :";cin>>MyExpenses.nEdSelf;

cout<<"Medical :";cin>>MyExpenses.nMed;

cout<<"Insurance :";cin>>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;

fprintf(fileOut,

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

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

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

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

     MyExpenses.nInsur);

cout<<"\n\nContinue <Y/N> ? ";cin>>cOption;

} while(cOption=='Y' || cOption=='y');

fclose(fileOut);

getch();

}


 


 


 

/* Program using Structure and output to file*/

/* Write multiple records */

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fileOut;

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;

int i;

char cOption;

fileOut = fopen("C:\\ex03.txt","w+");

clrscr();

cOption='Y';

do

{

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

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

cout<<"INCOME\n";

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

cout<<"Salary :";cin>>MyIncome.nSalary;

cout<<"Rent Received :";cin>>MyIncome.nRentRec;

cout<<"Other Income :";cin>>MyIncome.nOtherIn;

cout<<"EXPENSES\n\n";

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

cout<<"Rent Paid :";cin>>MyExpenses.nRentPaid;

cout<<"Public Utility :";cin>>MyExpenses.nPUB;

cout<<"Telephone :";cin>>MyExpenses.nTele;

cout<<"Education-Kids :";cin>>MyExpenses.nEdKid;

cout<<"Education-Self :";cin>>MyExpenses.nEdSelf;

cout<<"Medical :";cin>>MyExpenses.nMed;

cout<<"Insurance :";cin>>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;

fprintf(fileOut,

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

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

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

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

     MyExpenses.nInsur);

cout<<"\n\nContinue <Y/N> ? ";cin>>cOption;

} while(cOption=='Y' || cOption=='y');

fclose(fileOut);

getch();

}

Monday, July 20, 2009

Monthly Expenses program using structure (formatted)

Monthly Expenses using Structure and File (Source Code)

This is the same post as previous just coding is formatted here. Please note the comments in data member declaration. Further this program will write the data into a text file.


From this post, I prefer to have the source code as Google documents link only. You can download the file.

I just want to point out that you can change the file mode, if you prefer.

Cheers!!!

Monthly Expenses program using structure

Monthly Expenses program using structure (Source Code)

The c++ program in the previous post is improved here. I used structure to group the Income and Expenses data. I hope you can understand the program easily.


#include <>
#include <>
void main()
{
struct income {
float nSalary, nRentRec, nOtherIn;
};
struct expenses {
float nRentPaid, nPUB, nTele, nEdKid, nEdSelf, nMed, nInsur;
};
struct income MyIncome;
struct expenses MyExpenses;
float nInTotal, nExTotal, nBalance;
clrscr();
cout<<"Monthly Expenses Vs Income\n"; cout<<"--------------------------\n"; cout<<"INCOME\n"; cout<<"======\n"; cout<<"Salary :";cin>>MyIncome.nSalary;
cout<<"Rent Received :";cin>>MyIncome.nRentRec;
cout<<"Other Income :";cin>>MyIncome.nOtherIn;
cout<<"EXPENSES\n"; cout<<"========\n"; cout<<"Rent Paid :";cin>>MyExpenses.nRentPaid;
cout<<"Public Utility :";cin>>MyExpenses.nPUB;
cout<<"Telephone :";cin>>MyExpenses.nTele;
cout<<"Education-Kids :";cin>>MyExpenses.nEdKid;
cout<<"Education-Self :";cin>>MyExpenses.nEdSelf;
cout<<"Medical :";cin>>MyExpenses.nMed;
cout<<"Insurance :";cin>>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 : "<<>
cout<<"\nTotal Expenses : "<<>
if (nBalance<0)><>