Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > the counter

Reply
Thread Tools

the counter

 
 
CuTe_Engineer
Guest
Posts: n/a
 
      10-07-2007
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:rint() 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[i].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[i].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

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      10-09-2007

"CuTe_Engineer" wrote:

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:rint() 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[i].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[i].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.
>
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Reset network creation counter J.H. Holliday Wireless Networking 4 06-11-2005 08:09 PM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
Java Applet counter doesn't display correctly Dick Stouffer Firefox 8 10-05-2004 04:52 PM
can I do such a simplest counter in VHDL? walala VHDL 3 09-14-2003 08:05 PM



Advertisments