calculate how many jellybeans can fit in jar

all my calculations keep coming wrong. I tried asking my teacher but no matter how changes I make the calculation keeps coming out wrong. I would greatly appreciate if anybody can help me fix this program.



#include <iostream>
#include <cmath>

using namespace std;

int main() {
/*
Antonio Carlos Rodrigues
arodriguez245@cnm.edu
RodriguesP1
*/
double height, diameter, glassThickness, radius, newRadius, subGlassFromDiam, volumeOfJar, airCubicInches, trueVolume, jellyBeanVolume;
int jellyBeanSpace;

cout << "Name: Antonio Rodrigues" << endl;
cout << "Email: arodriguez245@cnm.edu" << endl;
cout << "File name: RodriguesP1" << endl;

cout << "project name: jellybean claculator" << endl;
cout << endl << "Hello user, the objective of this program is to calculate how many jellybeans can fit in a jar." << endl;
cout << "Down below you will be able to enter your jars height, diameter and the thickness of the jar's glass." << endl;


cout << endl << "Enter jar's height: ";
cin >> height;

cout << "Enter jar's diameter: " ;
cin >> diameter;
radius = (diameter / 2);
cout << "Radius of the jar is " << radius << endl;

cout << "Enter the thickness of the jar's glass: ";
cin >> glassThickness;
//subGlassFromDiam is subtracting the glass from the diameter
newRadius = (radius - glassThickness);


volumeOfJar = (((newRadius*newRadius) *height)*3.14159);
cout << endl <<"The volume of the jar is "<< volumeOfJar << endl;

airCubicInches = (volumeOfJar*.20);
trueVolume = (volumeOfJar - airCubicInches);
cout << endl << "Volume of jar with the calculated air space or packing space is " << trueVolume << endl;

jellyBeanVolume = (4/3*(3.14*pow(0.25, 2)*0.625));
cout<< endl << "The volume of each jelly bean is" << jellyBeanVolume <<endl;
jellyBeanSpace = (trueVolume / jellyBeanVolume);
cout<< endl << "The amount of jelly that can fit in your jar is " << jellyBeanSpace << endl;

cout << "Thank you for using the jellybean app goodbye";

return 0;
}
Your jellyBeanVolume will be wrong because of integer division: 4/3 will give an integer, which is 1 . Including a decimal point in one number would solve that: e.g. 4.0/3 .
I don't recognise the rest of the formula for a jellybean volume. If you are modelling a jellybean as a cylinder plus two hemispheres then the formula as coded is wrong. I'd expect it to be more like (volume of sphere + volume of cylinder):
(4.0/3)pi.radius3+pi.radius2.length
If 3.14 is meant to be pi, then it's inaccurate and inconsistent with what you used earlier. You should try to avoid magic numbers; e.g. write jellyBeanRadius instead of just 0.25: it makes it easier to see what is going on and easier to change values in one place rather than many.

You print out lots of intermediate results (which is good). So why not hand check them to see which equation goes wrong?

So:
- put your code in code tags (select the code and use the Format palette to the right);
- give us a sample input and output;
- check your formula for the volume of a jellybean.
Last edited on
Topic archived. No new replies allowed.