Im into the last stages of my C++ class and am having trouble with a project that has been assigned. Here is the problem and thanks in advance for whoever helps out!

Declare an object of type bucket

1). Use the SetGallonSize() method to set the bucket size to 5.0 gallons

2). Use the FillBucket() method to fill the bucket to 3.2 gallons of water

3). Use the GetWeight() method to output the weight of the 3.2 gallons of water

Here is the header file that was provided

// bucket.h

#ifndef _BUCKET_H
#define _BUCKET_H

const double WEIGHT_OF_WATER = 8.3;

class bucket
{
  public:
	 // constructors
	 bucket();
	 bucket(bucket & Object);

	 // member functions
	 void  SetGallonSize(double Size);
	 void  FillBucket(double Amount);
	 double GetWeight();

  private:
	 // data
	 double MySize,
	       MyAmount;

};

bucket::bucket()
{
  MySize = 0;
  MyAmount = 0;
}

bucket::bucket(bucket & Object)
{
  MySize = Object.MySize;
  MyAmount = Object.MyAmount;
}

void bucket::SetGallonSize(double Size)
{
  MySize = Size;
}

void bucket::FillBucket(double Amount)
{
  if(Amount <= MySize)
  {
    MyAmount = Amount;
  }
  else
  {
    MyAmount = MySize;
  }
}

double bucket::GetWeight()
{
  return(MyAmount*WEIGHT_OF_WATER);
}

#endif

Here is my code I have so far. I dont know if Im on the right track, because the output is 53.20.

// bucket.cpp
#include <iostream>
#include <bucket.h>
using namespace std;

int main ()
{
	bucket Bucket_Size;
	bucket Bucket_Amount;
	double MySize = 5.0;
	double MyAmount = 3.2;

	
	Bucket_Size.SetGallonSize(MySize);
	
	
	Bucket_Amount.FillBucket (MyAmount);
	

	
	cout << "The bucket amount is " << MySize << MyAmount << Bucket_Size.GetWeight() << endl << endl;
	
	return 0;
}

Recommended Answers

All 3 Replies

You have 2 problems:
First, you have the same problem as in your previous thread. Unless you're some sort of prodigy, you can't just barf something into your code and expect it to work correctly. You need to start taking the time to think about how you write your output statements. Just like before, you've crammed all of your output together without any sense of formatting. Thus, the output you are getting is correct output for how you have written your statement(s).

Second, when working with objects, you create a variable to represent the object, not a variable for each part of an object. Your variables "Bucket_Size" and "Bucket_Amount" are each individual instances of bucket with their own unique sets of data. They are in no way linked to each other. You should only have one (1) bucket called something like "myBucket". You would then call the sub-members of that single bucket object.

//declare some configuration constants
const double CAPACITY = 6.0;
const double INITIAL_FILL = 2.5;

//declare the container
container myContainer;

//configure the container
myContainer.setCapacity(CAPACITY);
myContainer.fill(INITIAL_FILL);

//show the container's current status
cout << "My container has a capacity of: " << myContainer.getCapacity()
     << " gallons, and is filled with " << myContainer.getFill() << " gallons." << endl;

Notice how there is only one (1) container that is used 4 times. There are not 4 different containers.

You have 2 problems:
First, you have the same problem as in your previous thread. Unless you're some sort of prodigy, you can't just barf something into your code and expect it to work correctly. You need to start taking the time to think about how you write your output statements. Just like before, you've crammed all of your output together without any sense of formatting. Thus, the output you are getting is correct output for how you have written your statement(s).

Second, when working with objects, you create a variable to represent the object, not a variable for each part of an object. Your variables "Bucket_Size" and "Bucket_Amount" are each individual instances of bucket with their own unique sets of data. They are in no way linked to each other. You should only have one (1) bucket called something like "myBucket". You would then call the sub-members of that single bucket object.

//declare some configuration constants
const double CAPACITY = 6.0;
const double INITIAL_FILL = 2.5;

//declare the container
container myContainer;

//configure the container
myContainer.setCapacity(CAPACITY);
myContainer.fill(INITIAL_FILL);

//show the container's current status
cout << "My container has a capacity of: " << myContainer.getCapacity()
     << " gallons, and is filled with " << myContainer.getFill() << " gallons." << endl;

Notice how there is only one (1) container that is used 4 times. There are not 4 different containers.

Well as you know I'm no prodigy (dont answer that). Here is my new code. If you noticed in my header file there is no getCapacity(), or in this case getSize() so I just used Size in my output statement.

#include <iostream>
#include <bucket.h>
using namespace std;


//declared constants
 const double Size = 5.0;
 const double Fill = 3.2;

 int main()
{
//declare type bucket
 bucket My_Bucket;
 


//configure the bucket
My_Bucket.SetGallonSize (Size);
My_Bucket.FillBucket (Fill);

//calculate weight of bucket with water
cout << "My bucket has a capacity of: " << Size
     << " gallons, and is filled with " << My_Bucket.GetWeight() << " gallons of water." << endl;

return 0;
}

I suggest you take some more time to think about your output statement. The example I posted in no way meets the requirements of your assignment. It was inappropriate of you to assume so and subsequently copy/paste it into your homework. How do you plan to justify this invalid output to your professor:

My bucket has a capacity of: 5.0 gallons, and is filled with 26.56 gallons of water.

Here's a hint, you can't.

You need to rewrite it to meet your assignment's requirements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.