CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    8

    Question You don't seem to be initializing the amount data member properly.

    I need help I cant find why amount is not initializing properly, here is the problem:

    Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is:
    A data member named amount of type double.
    An data member named capacity of type double.
    A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity .
    A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity , amount is set to capacity .
    A function named useGas that accepts a parameter of type double . The value of the amount data member is decreased by the value of the parameter. However, if the value of amount is decreased below 0 , amount is set to 0 .
    A function named isEmpty that accepts no parameters and returns a boolean value. isEmpty returns true if the value of amount is less than 0.1 , and false otherwise.
    A function named isFull that accepts no parameters and returns a boolean value.. isFull returns true if the value of amount is greater than capacity-0.1 , and false otherwise.
    A function named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount data member.

    here is my code....idk what im doing wrong i followed every direction:
    GasTank::GasTank(double a)
    {
    capacity = a;
    }

    void GasTank::addGas(double a)
    {
    amount += a;
    if(amount > capacity)
    {
    amount = capacity;
    }
    }

    void GasTank::useGas(double a)
    {
    amount -= a;
    if(amount < 0)
    {
    amount = 0;
    }
    }

    bool GasTank::isEmpty()
    {
    if(amount < 0.1)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    bool GasTank::isFull()
    {
    if(amount > (capacity-0.1))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    double GasTank::getGasLevel()
    {
    return amount;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: You don't seem to be initializing the amount data member properly.

    Quote Originally Posted by mejiac3 View Post
    I need help I cant find why amount is not initializing properly, here is the problem:
    1) Use code tags when posting code. The code you posted is unformatted.

    2) The reason why amount is not initialized is because you didn't initialize it. Very simple. Where in that code do you initialize amount?

    Is amount a class member variable? If it is, then where do you set it to 0 or whatever you need to set it to before using it?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2012
    Posts
    8

    Thumbs up Re: You don't seem to be initializing the amount data member properly.

    facepalm on me....wow i seriously thought i initialize amount in the constructor but that was just capacity. thank you when i did this i was half asleep it seems, thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured