what is my mistake

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

  1. hii,

    can you tell me plzz why my programme doesn`t work ,i don`t have any
    errors and every thing is fine but i don`t know why it`s not
    working ,
    soo plz can you help me un finding my mistake
    i will past the proramme here ,


    the headers,


    #include<string>





    #ifndef H_Gymnasium
    #define H_Gymnasium


    struct activities
    {
    string exercises;
    int time;



    };


    class Gymnasium
    {
    protected:
    activities gymarray[5];
    int gymlength;
    public:
    void set(string,int);
    void getActivities(string&,int&)const;
    double calories();
    void print();
    Gymnasium(string="", int=0 );

    };


    #endif

    #include<string>





    #ifndef H_Nutrition
    #define H_Nutrition


    struct meals
    {
    string food;
    int portion;



    };


    class Nutrition
    {
    protected:
    meals nutritionarray[5];
    int nutritionLength;

    public:
    void set(string,int);
    void getMeals(string& , int&)const;
    void print();
    Nutrition(string="" , int=0);
    double calories();


    };


    #endif

    #include <string>
    #include"Regime.h"


    #ifndef H_Patient
    #define H_Patient





    struct person
    {
    string name;
    double energy;



    };


    class Patient:public Gymnasium , public Nutrition,public Regime
    {

    public:
    void set(string,int,string,int,string,double);
    void get(string&,int& ,string& ,int&,string&,double&)const;
    void print();
    Patient(string= "",int=0,string="",int=0,string=0,double=0);
    double calories(); // find the difference between gained & lost
    calories and
    //and sub. it from the energy
    private:
    person info;
    Regime regimePlan;


    };


    #endif

    #include<string>
    #include"Gymnasium.h"
    #include"Nutrition.h"


    #ifndef H_Regime
    #define H_Regime





    class Regime
    {
    public:
    void set(string,int ,string,int);
    void print();
    Regime(string="" ,int=0,string="",int=0);
    double calories();


    private:
    Gymnasium exercisePlan ;
    Nutrition dietPlan;


    };


    #endif
    ===============================================
    the implitation

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


    using namespace std;


    const int gymlength=5;


    void Gymnasium::set(string ex , int T )
    {


    gymarray[gymlength].exercises=ex;
    gymarray[gymlength].time=T;



    }


    void Gymnasium::getActivities(string& ex , int& T )const
    {

    ex=gymarray[gymlength].exercises;
    T=gymarray[gymlength].time;



    }


    void Gymnasium::print()
    {
    cout<<"He/She lost "<<calories()<<"calories"<<endl;


    }


    Gymnasium::Gymnasium(string ex , int T )
    {

    gymarray[gymlength].exercises=ex;
    gymarray[gymlength].time=T;



    }


    double Gymnasium:: calories()
    {
    int total,calories ,i,lostcalories;

    for(i=0;i>gymlength;i++)
    {


    if (gymarray.exercises=="OpenGym"){
    calories=680*gymarray.time;


    }
    else if (gymarray.exercises=="Treadmill"){
    calories=820*gymarray.time;


    }
    else if (gymarray.exercises=="Bike"){
    calories=620*gymarray.time;


    }
    else if (gymarray.exercises=="Fitness"){
    calories=740*gymarray.time;


    }
    else if (gymarray.exercises=="Hoop"){
    calories=210*gymarray.time;
    }
    total+=calories;


    }


    lostcalories=total/60;
    return lostcalories;



    }


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

    using namespace std;


    const int Nutritionlength=4;


    void Nutrition::set(string f , int p )
    {
    nutritionarray[Nutritionlength].food=f;
    nutritionarray[Nutritionlength].portion=p;



    }


    void Nutrition ::getMeals(string&f , int&p )const
    {
    f=nutritionarray[Nutritionlength].food;
    p=nutritionarray[Nutritionlength].portion;


    }


    Nutrition::Nutrition(string f , int p )
    {
    set(f,p);


    }


    double Nutrition::calories()
    {
    int total,calories ,i,gainedcalories;

    for(i=0;i>4;i++)
    {
    nutritionarray.food;
    nutritionarray.portion;


    if (nutritionarray.food=="Carbohydrates"){
    calories=90*nutritionarray.portion;


    }
    else if (nutritionarray.food=="Protein"){
    calories=70*nutritionarray.portion;


    }
    else if (nutritionarray.food=="Vitamins"){
    calories=50*nutritionarray.portion;


    }
    else if (nutritionarray.food=="Water"){
    calories=10*nutritionarray.portion;


    }


    total+=calories;
    }


    gainedcalories=total/100;


    return gainedcalories;


    }


    void Nutrition:: print()
    {
    cout<<"He/She gained "<<calories()<<"calories"<<endl;


    }


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

    using namespace std;


    void Patient::set(string ex,int T,string f,int p,string n,double e)
    {
    Gymnasium::set(ex,T);
    Nutrition::set(f,p);
    info.name=n;
    info.energy=e;



    }


    void Patient::get(string& ex,int& T ,string& f ,int& p,string&
    n,double&e)const
    {
    Gymnasium::getActivities(ex,T);
    Nutrition::getMeals(f,p);
    n=info.name;
    e=info.energy;


    }


    void Patient::print()
    {

    cout<<"Before the regime"<<info.name<<"energy was
    "<<calories()<<"calories"<<endl;
    Regime::print();
    Gymnasium::print();
    Nutrition::print();



    }


    Patient::patient(string ex,int T,string f,int p,string n,double
    e):Gymnasium(ex,T),Nutrition(f,p),
    Regime(ex,T,f,p)
    {
    n=info.name;
    e=info.energy;


    }


    double Patient::calories()
    {
    int before;

    before=Regime::calories()+600;


    return before;



    }


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

    using namespace std;


    void Regime::set(string ex,int T,string f,int p)
    {
    exercisePlan.set(ex,T);
    dietPlan.set(f,p);



    }


    void Regime::print()
    {
    cout<<"After the regime Ahmed energy is
    "<<calories()<<"calories"<<endl;


    }


    Regime::Regime(string ex,int T,string f,int p)
    {
    exercisePlan.set(ex,T);
    dietPlan.set(f,p);


    }


    double Regime::calories()
    {
    int After;

    After=dietPlan.calories()- exercisePlan.calories();


    return After;


    }


    ================================
    the driver
    #include<iostream>
    #include<string>
    #include<fstream>
    #include"Gymnasium.h"
    #include"Nutrition.h"
    #include"Patient.h"
    #include"Regime.h"

    using namespace std;


    int main()
    {
    Patient A;


    int T,p ;
    string f , ex , n;
    double e;


    cin>>n>>e;
    cout<<endl;


    ifstream inFile;


    inFile.open("patientActivities.txt");


    inFile>>ex>>T;


    inFile.close();


    inFile.open("patientMeals.txt");


    inFile>>f>>p;


    inFile.close();


    A.set(ex,T,f,p,n,e);


    A.print();


    return 0;
    CuTe_Engineer, Oct 28, 2007
    #1
    1. Advertising

  2. CuTe_Engineer a écrit :
    > hii,
    >
    > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > errors and every thing is fine but i don`t know why it`s not
    > working ,


    Everything is not fine because the program compiles.

    You should learn how to use arrays. I suspect you try to use C++ as a
    dynamically typed language and expect arrays to be smart.

    Example from your code bellow:

    > activities gymarray[5];


    > const int gymlength=5;
    > void Gymnasium::set(string ex , int T )
    > {
    >
    >
    > gymarray[gymlength].exercises=ex;
    > gymarray[gymlength].time=T;


    index of an array should be 0 <= index < gymlength.
    Here you are writing outside the array.

    Writing at the end of an array doesn't increase it size.

    > }


    > void Gymnasium::getActivities(string& ex , int& T )const
    > {
    >
    > ex=gymarray[gymlength].exercises;
    > T=gymarray[gymlength].time;


    Here again you get a value outside the array.

    I suspect you were expecting to tables: one of of exercises and one of
    times. I doen't work this way: from an array of activities you can only
    access individual elements (gymarray).

    So you method must return a specific element in the array or you have to
    construct the arrays you want.

    > }
    >


    Michael
    Michael DOUBEZ, Oct 28, 2007
    #2
    1. Advertising

  3. On Oct 28, 9:51 am, Michael DOUBEZ <> wrote:
    > CuTe_Engineer a écrit :
    >
    > > hii,

    >
    > > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > > errors and every thing is fine but i don`t know why it`s not
    > > working ,

    >
    > Everything is not fine because the program compiles.
    >
    > You should learn how to use arrays. I suspect you try to use C++ as a
    > dynamically typed language and expect arrays to be smart.
    >
    > Example from your code bellow:
    >
    > > activities gymarray[5];
    > > const int gymlength=5;
    > > void Gymnasium::set(string ex , int T )
    > > {

    >
    > > gymarray[gymlength].exercises=ex;
    > > gymarray[gymlength].time=T;

    >
    > index of an array should be 0 <= index < gymlength.
    > Here you are writing outside the array.
    >
    > Writing at the end of an array doesn't increase it size.
    >
    > > }
    > > void Gymnasium::getActivities(string& ex , int& T )const
    > > {

    >
    > > ex=gymarray[gymlength].exercises;
    > > T=gymarray[gymlength].time;

    >
    > Here again you get a value outside the array.
    >
    > I suspect you were expecting to tables: one of of exercises and one of
    > times. I doen't work this way: from an array of activities you can only
    > access individual elements (gymarray).
    >
    > So you method must return a specific element in the array or you have to
    > construct the arrays you want.
    >
    > > }

    >
    > Michael


    so it should be two dimensional array so that i`l be able to access
    the exercise and the time right?
    like:
    gymarray[j]
    CuTe_Engineer, Oct 28, 2007
    #3
  4. CuTe_Engineer wrote:
    > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > errors and every thing is fine but i don`t know why it`s not
    > working ,
    > soo plz can you help me un finding my mistake
    > i will past the proramme here ,


    Would it be too much to ask if you tried to find the problem yourself
    first and only then, after not being successful, you could ask properly
    by
    1) telling what is it that the program is supposed to do,
    2) telling how it is malfunctioning
    3) telling what you tried to do, or where do you think the mistake
    might be,
    4) posting a *minimal* version of the program which reproduces the
    problem instead of just dumping the the entire program, without any
    info, without the slightest sign of you even having tried to solve the
    problem yourself, so that others will do the hard work for you.

    You cannot always rely on other people solving your problems. You have
    to learn to solve them yourself, and also to at least *try* to solve
    them and if you don't succeed, to at least do the majority of the work
    hunting the bug so that you can ask others properly about the problem
    and not just burden them with the entire thing.
    Juha Nieminen, Oct 28, 2007
    #4
  5. CuTe_Engineer

    Dave Shooter Guest

    CuTe_Engineer wrote:
    > hii,
    >
    > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > errors and every thing is fine but i don`t know why it`s not
    > working ,
    > soo plz can you help me un finding my mistake
    > i will past the proramme here ,


    <snip>

    > class Patient:public Gymnasium , public Nutrition,public Regime
    > {
    > public:
    > void set(string,int,string,int,string,double);
    > void get(string&,int& ,string& ,int&,string&,double&)const;
    > void print();
    > Patient(string= "",int=0,string="",int=0,string=0,double=0);
    > double calories(); // find the difference between gained & lost
    > calories and
    > //and sub. it from the energy
    > private:
    > person info;
    > Regime regimePlan;
    > };


    <snip>

    Public inheritance models an 'is-a' relationship between entities where,
    generally, the derived class (eg Patient) is a specialised version of
    the more general base class. The Patient declaration here effectively
    states that a Patient is a kind of Gymnasium, is a kind of Nutrition and
    is a kind of Regime. Obviously I don't have the specification for your
    program, but in terms of Object-Orientated Design, the use of multiple
    inheritance here doesn't strike me as a correct design choice.
    Dave Shooter, Oct 28, 2007
    #5
  6. On Oct 28, 12:20 pm, Juha Nieminen <> wrote:
    > CuTe_Engineer wrote:
    > > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > > errors and every thing is fine but i don`t know why it`s not
    > > working ,
    > > soo plz can you help me un finding my mistake
    > > i will past the proramme here ,

    >
    > Would it be too much to ask if you tried to find the problem yourself
    > first and only then, after not being successful, you could ask properly
    > by
    > 1) telling what is it that the program is supposed to do,
    > 2) telling how it is malfunctioning
    > 3) telling what you tried to do, or where do you think the mistake
    > might be,
    > 4) posting a *minimal* version of the program which reproduces the
    > problem instead of just dumping the the entire program, without any
    > info, without the slightest sign of you even having tried to solve the
    > problem yourself, so that others will do the hard work for you.
    >
    > You cannot always rely on other people solving your problems. You have
    > to learn to solve them yourself, and also to at least *try* to solve
    > them and if you don't succeed, to at least do the majority of the work
    > hunting the bug so that you can ask others properly about the problem
    > and not just burden them with the entire thing.


    okey sorry and i`ll not ASK you to solve my problems

    > 3) telling what you tried to do, or where do you think the mistake
    > might be,


    if i know where are my mistake i`ll not ask you for a help

    > 1) telling what is it that the program is supposed to do,

    i think if you read what i wrote you will know what is it that the
    program is supposed to do

    > and also to at least *try* to solve
    > them and if you don't succeed, to at least do the majority of the work
    > hunting the bug so that you can ask others properly about the problem
    > and not just burden them with the entire thing.


    actualy i did the majority of the work but the programme it`s not
    working if i know why i won`t ask for help
    CuTe_Engineer, Oct 28, 2007
    #6
  7. CuTe_Engineer

    osmium Guest

    "Dave Shooter" wrote:

    > CuTe_Engineer wrote:
    >> hii,
    >>
    >> can you tell me plzz why my programme doesn`t work ,i don`t have any
    >> errors and every thing is fine but i don`t know why it`s not
    >> working ,
    >> soo plz can you help me un finding my mistake
    >> i will past the proramme here ,

    >
    > <snip>
    >
    >> class Patient:public Gymnasium , public Nutrition,public Regime
    >> {
    >> public:
    >> void set(string,int,string,int,string,double);
    >> void get(string&,int& ,string& ,int&,string&,double&)const;
    >> void print();
    >> Patient(string= "",int=0,string="",int=0,string=0,double=0);
    >> double calories(); // find the difference between gained & lost
    >> calories and
    >> //and sub. it from the energy
    >> private:
    >> person info;
    >> Regime regimePlan;
    >> };

    >
    > <snip>
    >
    > Public inheritance models an 'is-a' relationship between entities where,
    > generally, the derived class (eg Patient) is a specialised version of
    > the more general base class. The Patient declaration here effectively
    > states that a Patient is a kind of Gymnasium, is a kind of Nutrition and
    > is a kind of Regime. Obviously I don't have the specification for your
    > program, but in terms of Object-Orientated Design, the use of multiple
    > inheritance here doesn't strike me as a correct design choice.


    In particular, note that a gymnasium is a building and a patient is a human
    being. Note also that a patient "is -a" Regime and it also *has* a Regime.
    Usually, it would be one or the other ("is-a", "has-a"). If you were
    earlier given a less cumbersome assignment to learn inheritance, now would
    be a good time to go back and do it.
    osmium, Oct 28, 2007
    #7
  8. CuTe_Engineer

    osmium Guest

    "CuTe_Engineer" wrote:


    > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > errors and every thing is fine but i don`t know why it`s not
    > working ,


    <snip about 300 lines of code>

    > int main()
    > {
    > Patient A;


    You have just constructed a Patient with six default parameters, in other
    words, a patient about whom nothing whatsoever is known. What it the proper
    output of your program when this happens? In American idiom, my response
    would be "You have got to be kidding!". I note that the parameters include
    such meaningless variables such as f, T, and p.

    What useful information could *any* program possibly compute when no input
    is provided? You get some data from the user but then you proceed to
    totally ignore it. It is crucial that the order in which things are done is
    proper.

    If the instructor told you that a default constructor must be possible, you
    will have to, by program, detect the nonsensical attempts to use the end
    result of such a constructor. I would defer this work to the end after the
    main program is pretty well working. This is the wrong order of work for a
    real programmer, but you are not a real programmer.
    ----
    As side note, you show positive calorie consumption for water. The calorie
    consumption for water is negative since the water must be warmed up and then
    excreted, thus wasting energy.
    osmium, Oct 28, 2007
    #8
  9. CuTe_Engineer wrote:
    > okey sorry and i`ll not ASK you to solve my problems


    That kind of attitude is not the best one if you really want people to
    help you.
    "Here's the program, it doesn't work, fix it" is an obnoxious request.

    > actualy i did the majority of the work but the programme it`s not
    > working if i know why i won`t ask for help


    You did not tell anything about what you have done, what the program
    is supposed to do and how it's misbehaving. You have to help people to
    help you.
    Juha Nieminen, Oct 28, 2007
    #9
  10. CuTe_Engineer

    BobR Guest

    CuTe_Engineer wrote in message...

    /* """
    On Oct 28, 9:51 am, Michael DOUBEZ <> wrote:
    > CuTe_Engineer a écrit :
    > > hii,
    > > can you tell me plzz why my programme doesn`t work ,i don`t have any
    > > errors and every thing is fine but i don`t know why it`s not
    > > working ,

    >
    > Everything is not fine because the program compiles.
    >
    > You should learn how to use arrays. I suspect you try to use C++ as a
    > dynamically typed language and expect arrays to be smart.
    >
    > Example from your code bellow:
    >
    > > activities gymarray[5];
    > > const int gymlength=5;
    > > void Gymnasium::set(string ex , int T ){
    > > gymarray[gymlength].exercises=ex;
    > > gymarray[gymlength].time=T;

    >
    > index of an array should be 0 <= index < gymlength.
    > Here you are writing outside the array.
    >
    > Writing at the end of an array doesn't increase it size.
    >
    > > }
    > > void Gymnasium::getActivities(string& ex , int& T )const{
    > > ex=gymarray[gymlength].exercises;
    > > T=gymarray[gymlength].time;

    >
    > Here again you get a value outside the array.
    >
    > I suspect you were expecting to tables: one of of exercises and one of
    > times. I doen't work this way: from an array of activities you can only
    > access individual elements (gymarray).
    >
    > So you method must return a specific element in the array or you have to
    > construct the arrays you want.
    >
    > > }

    so it should be two dimensional array so that i`l be able to access
    the exercise and the time right?
    like:
    gymarray[j]

    """ */

    No. Your array is:

    activities gymarray[5];

    That means you can only access:

    gymarray[0]; // first element
    gymarray[1];
    gymarray[2];
    gymarray[3];
    gymarray[4]; // last element

    But, when you use gymlength (which == 5), then you are doing:

    gymarray[5];

    .... which is outside the array!

    --
    Bob R
    POVrookie
    BobR, Oct 29, 2007
    #10
  11. CuTe_Engineer

    osmium Guest

    cute_engineer wrote:

    > void Patient::get(string& ex,int& T ,string& f ,int& p,string&
    > n,double&e)const
    > {
    > Gymnasium::getActivities(ex,T);
    > Nutrition::getMeals(f,p);
    > n=info.name;
    > e=info.energy;


    The '=' symbol does *not* mean equal. Try to figure out what it does mean
    and incorporate your findings into your program.
    > }
    osmium, Oct 29, 2007
    #11
  12. CuTe_Engineer

    arnuld Guest

    > On Sun, 28 Oct 2007 05:45:19 -0700, CuTe_Engineer wrote:


    > okey sorry and i`ll not ASK you to solve my problems


    Aye... What should I think of you ?


    > if i know where are my mistake i`ll not ask you for a help


    What kind of man you are ?

    So you are ... CuTe_Engineer.... hmmm.....

    let me think ....

    ...... you seem like a TROLL to me
    arnuld, Oct 31, 2007
    #12
    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. \(beta-\) Frank Nitzsche

    where is the mistake?

    \(beta-\) Frank Nitzsche, Jun 25, 2004, in forum: VHDL
    Replies:
    4
    Views:
    534
  2. Jesse

    Beginner Mistake

    Jesse, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    361
    Jesse
    Jul 22, 2003
  3. Joerg Jooss
    Replies:
    1
    Views:
    403
    DotNetJunkies User
    May 22, 2004
  4. John
    Replies:
    6
    Views:
    646
  5. Leon

    Does anyone see my mistake?

    Leon, Nov 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    333
Loading...

Share This Page