the counter

Discussion in 'C++' started by CuTe_Engineer, Oct 7, 2007.

  1. 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
    CuTe_Engineer, Oct 7, 2007
    #1
    1. Advertising

  2. CuTe_Engineer

    osmium Guest

    "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::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.
    >
    > 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
    >
    osmium, Oct 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    10,095
    VhdlCohen
    Sep 14, 2003
  2. Jan Kindt
    Replies:
    1
    Views:
    1,477
    Jim Lewis
    Sep 17, 2003
  3. dong seok huh

    [Q] : async event counter

    dong seok huh, Oct 1, 2003, in forum: VHDL
    Replies:
    6
    Views:
    749
    Renaud Pacalet
    Oct 6, 2003
  4. The Eeediot
    Replies:
    3
    Views:
    2,214
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  5. George2
    Replies:
    1
    Views:
    781
    Alf P. Steinbach
    Jan 31, 2008
Loading...

Share This Page