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; }
-
Answer:
Just as the compiler says, you aren't initialising 'amount'. Look at where you use 'amount' in the program. Every place you do that, you do something that requires the program to modify or examine 'amount' in some way ("add 'a' to 'amount'", "is 'amount' less than 0.1" and so on), but there's no place where you ever simply assign a value to 'amount'. You've followed the specification faithfully, but unfortunately the specification is incomplete, so it's up to you to fill in the gap if you want a working program. The obvious place to initialise 'amount' is in the constructor, where currently you only assign a value to 'capacity'. Since the specification doesn't say anything about the initial amount the simplest solution is just to add: amount = 0; in the constructor.
Christia... at Yahoo! Answers Visit the source
Related Q & A:
- Would someone at the underground stations lose their jobs if we don't drop litter?Best solution by Yahoo! Answers
- Why does the Easter bunny carry eggs? Rabbits don't lay eggs.?Best solution by Yahoo! Answers
- Why don't I get the little envelope in my taskbar in Outlook Express when I get a message?Best solution by Yahoo! Answers
- How can you erase a link on the tab that I don't want to have anymore?Best solution by Yahoo! Answers
- What happens to your body when you don't eat?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.