the counter

C

CuTe_Engineer

hii,

i wrote this prog

//header file


#include <string>

using namespace std;

class expenditure
{
public: //public member function
void set(string, int,int,int);
void get(string&, int&,int&,int&) const;
static int getCount();
static int gettotalexpenditure( );
void print() const;
expenditure (string = "Al-Mullah", int= 800, int = 2400, int= 1200);
~ expenditure ();
static double averageExpenditure();

private: // private member function
string name;
int utilities ;
int food;
int personal;
int subtotal;
static int count;
static int totalexpenditure ;
};

// ExpenditureImp.cpp



#include<iostream>
#include<string>
#include"Ex.h"

using namespace std;

int expenditure :: count=0;
int expenditure ::totalexpenditure=0;

void expenditure::set(string N , int u , int f, int p) // set
function
{

totalexpenditure -=subtotal;

name=N;
utilities = u;
food=f;
personal=p;

subtotal=food+personal+utilities;

totalexpenditure += subtotal;
}
void expenditure::get(string& N , int& u , int& f , int& p)const //
get function
{
N=name;
f=food;
p=personal;
u=utilities;
}
expenditure::expenditure(string N, int u, int f , int p) //
constructor
{

subtotal=0;
set(N,u,f,p);

count++;

}

int expenditure::getCount() // get count
{

return count;

}


int expenditure::gettotalexpenditure( ) //return totalexpenditure
{
return totalexpenditure;
}

expenditure::~expenditure() //distructor
{
count--;
totalexpenditure -= subtotal;
cout << "Number of families is: " << count << " and total Expenditure
is: " << totalexpenditure <<endl;
}

void expenditure::print() const // print
{

cout << name << " Expenditure is: " << subtotal << endl;
}
double expenditure::averageExpenditure( )
{
return totalexpenditure/count;

}

#include <iostream>
#include <fstream>
#include <string>
#include "Ex.h"

using namespace std;
const int arraySize = 10;
int main()
{
string N;
int p ,f ,u;
int i;
int numberoffamilies=0;
ifstream inFile("expenditure.txt", ios::in); // reading from file



expenditure A[arraySize]; // array
for(i=0; i<arraySize; i++)
{
A.print();
}

cout << "Number of families is: " << expenditure::getCount() << " and
total Expenditure is: " << expenditure::gettotalexpenditure( ) <<
endl;


while(inFile >>N >> u >> f >>p)
A[numberoffamilies++].set(N,u,f,p); // number of families
for(i=0; i<numberoffamilies; i++)
{
A.print();
}
cout << "Number of families is: " << expenditure::getCount() << "
and total Expenditure is: " << expenditure::gettotalexpenditure( ) <<
endl;
cout<<" Average families expenditure is:
"<<expenditure::averageExpenditure()<<endl;
return 0;

}

my Question is that when i want to start counting families from the
number 7 instead of 10 what should i do ?do i need to use a new
function or what ??

this is the output i want get

Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Al-Mullah expenditure is: 4400
Number of families is: 10 and total expenditure is: 44000
Al-Mullah expenditure is: 5100
Al-Issa expenditure is: 5300
Al-Roome expenditure is: 5457
Al-Shebani expenditure is: 4150
Al-Kurdi expenditure is: 4880
Al-Ali expenditure is: 4720
Al-Walled expenditure is: 3940
Number of families is: 7 and total expenditure is: 33547<<<< here is
my mistake instead of 7 i
Average families expenditure is:
4792.4 am getting 10

Total number of families is: 6 and total expanditure is: 29147
Total number of families is: 5 and total expanditure is: 24747
Total number of families is: 4 and total expanditure is: 20347
Total number of families is: 3 and total expanditure is: 16407
Total number of families is: 2 and total expanditure is: 11687
Total number of families is: 1 and total expanditure is: 6807
Total number of families is: 0 and total expanditure is: 2657
Total number of families is: -1 and total expanditure is: -2800
Total number of families is: -2 and total expanditure is: -8100
Total number of families is: -3 and total expanditure is: -13200
 
O

osmium

:

Note that // comments are a disaster on Usenet posts. They cause many
errors on compile.
As I understand it, the program doesn't work AND you want to change it, too.
I suggest doing one thing at a time, get it working first.
i wrote this prog

//header file


#include <string>

using namespace std;

class expenditure
{
public: //public member function
void set(string, int,int,int);
void get(string&, int&,int&,int&) const;
static int getCount();
static int gettotalexpenditure( );
void print() const;
expenditure (string = "Al-Mullah", int= 800, int = 2400, int= 1200);
~ expenditure ();
static double averageExpenditure();

private: // private member function
string name;
int utilities ;
int food;
int personal;
int subtotal;
static int count;
static int totalexpenditure ;
};

// ExpenditureImp.cpp



#include<iostream>
#include<string>
#include"Ex.h"

using namespace std;

int expenditure :: count=0;
int expenditure ::totalexpenditure=0;

void expenditure::set(string N , int u , int f, int p) // set
function
{

totalexpenditure -=subtotal;

name=N;
utilities = u;
food=f;
personal=p;

subtotal=food+personal+utilities;

totalexpenditure += subtotal;
}
void expenditure::get(string& N , int& u , int& f , int& p)const //
get function
{
N=name;
f=food;
p=personal;
u=utilities;
}
expenditure::expenditure(string N, int u, int f , int p) //
constructor
{

subtotal=0;
set(N,u,f,p);

count++;

}

int expenditure::getCount() // get count
{

return count;

}


int expenditure::gettotalexpenditure( ) //return totalexpenditure
{
return totalexpenditure;
}

expenditure::~expenditure() //distructor
{
count--;
totalexpenditure -= subtotal;
cout << "Number of families is: " << count << " and total Expenditure
is: " << totalexpenditure <<endl;
}

void expenditure::print() const // print
{

cout << name << " Expenditure is: " << subtotal << endl;
}
double expenditure::averageExpenditure( )
{
return totalexpenditure/count;

}

#include <iostream>
#include <fstream>
#include <string>
#include "Ex.h"

using namespace std;
const int arraySize = 10;
int main()
{
string N;
int p ,f ,u;
int i;
int numberoffamilies=0;
ifstream inFile("expenditure.txt", ios::in); // reading from file

Comment is wrong. You are *opening* a file. Print an error message if the
file doesn't open.
expenditure A[arraySize]; // array
for(i=0; i<arraySize; i++)
{
A.print();
}

cout << "Number of families is: " << expenditure::getCount() << " and
total Expenditure is: " << expenditure::gettotalexpenditure( ) <<
endl;


while(inFile >>N >> u >> f >>p)
A[numberoffamilies++].set(N,u,f,p); // number of families
for(i=0; i<numberoffamilies; i++)
{
A.print();
}


You are overwriting the data in the array. Shouldn't you also clear the
data collected during the first filling? (The two static variables.) You
went to the effort of writing a destructor and then you don't call it. Does
this make sense?
cout << "Number of families is: " << expenditure::getCount() << "
and total Expenditure is: " << expenditure::gettotalexpenditure( ) <<
endl;
cout<<" Average families expenditure is:
"<<expenditure::averageExpenditure()<<endl;
return 0;

}

my Question is that when i want to start counting families from the
number 7 instead of 10 what should i do ?do i need to use a new
function or what ??

Not sure I understand. How about reading and discarding the first 7
records? Does that make any sense to you? I doubt if you want a new
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top