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

Saturday, June 6, 2009

Monthly Expenses program in C

Monthly Expenses program in C (Source Code)

Here is a simple program to prepare your monthly budget. For simplicity, I suggest to have the following income

  • Salary
  • Rent received
  • Others (may include any other sources of income)
We will have the following recurring expenses

  • House Rent
  • Electricity (PUB)
  • Telephone
  • Education-Kids
  • Education-Self
  • Medical
  • Insurance

Now the problem is to find the surplus/deficit for the particular month is given below:

#include <>
#include <>
void main()
{
float nSalary, nRentRec, nOtherIn;
float nRentPaid, nPUB, nTele, nEdKid, nEdSelf, nMed, nInsur;
float nInTotal, nExTotal, nBalance;
clrscr();
cout<<"Monthly Expenses Vs Income\n";
cout<<"--------------------------\n";
cout<<"INCOME\n";
cout<<"======\n";
cout<<"Salary :";cin>>nSalary;
cout<<"Rent Received :";cin>>nRentRec;
cout<<"Other Income :";cin>>nOtherIn;
cout<<"EXPENSES\n";
cout<<"========\n";
cout<<"Rent Paid :";cin>>nRentPaid;
cout<<"Public Utility :";cin>>nPUB;
cout<<"Telephone :";cin>>nTele;
cout<<"Education-Kids :";cin>>nEdKid;
cout<<"Education-Self :";cin>>nEdSelf;
cout<<"Medical :";cin>>nMed;
cout<<"Insurance :";cin>>nInsur;
nInTotal = nSalary + nRentRec + nOtherIn;
nExTotal = nRentPaid + nPUB + nTele
+ nEdKid + nEdSelf + nMed + nInsur;
nBalance = nInTotal - nExTotal;
cout<<"\nTotal Income : "<<><<"\nTotal Expenses : "<<>
if (nBalance<0)
cout<<"\nDeficit : ";
else
cout<<"\nSurplus : ";
cout<<>

getch();
}

Introduction to this blog

Dear Krishnan,

Thanks a lot. I dedicate this blog to you. You are my well wisher, mentor and friend.


Dear Friends,

I created this blog to share my knowledge in C and C++ with like minded people. I planned the posts in this blog in the following structure

blogpost {
choose a simple task
explain procedure/algorithm to solve the task
write the code for basic functionality
list some enhancements
ask the friends to solve them
discuss the friends' solution
}

There may be a discussion of few preliminary concepts for each post.

I assure you my response to your comments. In deed I must thank you in advance for your comments

Cheers!!!

Karthiganesh
karthiganesh@yahoo.com