user1810514
user1810514

Reputation: 47

Can't get program to identify if user enters zero

Iv been working on a problem where i have to make a program that subtracts the volume of spheres and cylinders from the volume of a rectangular block. However i cannot get it to recognize if the user enters 0 for any of the dimensions.

#include <iostream>

using namespace std;

void confirmDimension(int dim, char what);
void confirmBubbles(int bubbles);
double sphereVolume(int r);
double cylinderVolume(int rad, int h);
double recVolume(int h, int l, int w);
double total(int w, int l, int h, int bubbles, double radbubble, int cylinders, double radcyl, double heightcyl);

int main()
{
double height, length, width, bubbles, bubRadius, holes, tot, radiuscyl, heightcyl;

cout << "Enter the height, length ,and width of the hunk of cheese in centimeters.";
cin >> height;
confirmDimension(height, 'ht');
cin >> length;
confirmDimension(length, 'lg');
cin >> width;
confirmDimension(width, 'wd');

cout << "How many spherical bubbles are present?";
cin >> bubbles;
confirmDimension(bubbles, 'bb');
cout << "What is the radius of the spherical bubbles in centimeters?";
cin >> bubRadius;
confirmDimension(bubRadius, 'rad');

cout << "How many cylindrical holes are present?";
cin >> holes;
confirmDimension(holes, 'hl');
cout << "What is the radius and height of the surface cylinders in centimeters?";
cin >> radiuscyl;
confirmDimension(radiuscyl, 'rc');
cin >> heightcyl;
confirmDimension(heightcyl, 'hc');

tot = total(width, length, height, bubbles, bubRadius, holes, radiuscyl, heightcyl);

cout << "The total volume of cheese present is " << tot << "cubic centimeters.";

return 0;
}

void confirmDimension(int dimension, char what)
{

if (what == 'ht')
{
    while (dimension <= 0)
    {
        cout << "The height of hunk of cheese must be greater than zero.";
        cin >> dimension;
    }
}

else if (what == 'lg')
{
    while (dimension <= 0)
    {
        cout << "The length of hunk of cheese must be greater than zero.";
        cin >> dimension;
    }
}

else if (what == 'wd')
{
    while (dimension <= 0)
    {
        cout << "The width of hunk of cheese must be greater than zero.";
        cin >> dimension;
    }
}

if (what == 'bb')
{
    while (dimension <= 0)
    {
        cout << "The number f spherical bubbles must be greater than zero.";
        cin >> dimension;
    }
}

if (what == 'rad')
{
    while (dimension <= 0)
    {
        cout << "The radius of spherical bubble must be greater than zero.";
        cin >> dimension;
    }
}

if (what == 'hl')
{
    while (dimension <= 0)
    {
        cout << "The number of cylindrical holes must be greater than zero.";
        cin >> dimension;
    }
}

if (what == 'rc')
{
    while (dimension <= 0)
    {
        cout << "The radius of the cylindrical holes must be greater than zero.";
        cin >> dimension;
    }
}

if (what == 'hc')
{
    while (dimension <= 0)
    {
        cout << "The height of thee cylindrical holes must be greater than zero.";
        cin >> dimension;
    }
}
}

void confirmBubbles(int bubbles)
{
while (bubbles <= 0)
    cout << "The number of spherical bubbles must be greater than 0.";
cin >> bubbles;
}

double sphereVolume(int r)
{
double volumes;
volumes = 4 / 3 * (3.14159)*r*r*r;
return volumes;
}

double cylinderVolume(int rad, int h)
{
double volumec;
volumec = 3.14159*rad*rad*h;
return volumec;
}

double recVolume(int h, int l, int w)
{
double volumer;
volumer = h*l*w;
return volumer;
}

 double total(int wid, int len, int hei, int bubbles, double radbubble, int cylinders,     double radcyl, double heightcyl)
{
double S, C, R, total;
S = sphereVolume(radbubble);
C = cylinderVolume(radcyl, heightcyl);
R = recVolume(wid, len, hei);
total = R - C - S;
return total;
}

Upvotes: 0

Views: 47

Answers (2)

Sudo Andrew
Sudo Andrew

Reputation: 92

you are checking to see if the input is a char when you are entering a string.

void confirmDimension(int dim, char what); should be void confirmDimension(int dim, string what);

then when you are calling confirmDimensions it should be something like

confirmDimensions(height, "ht");

instead of confirmDimensions(height, 'ht');

and you should change it likewise in the confirmDimensions function.

i.e. in the function it should read if (what == "ht") and not if (what =='ht')

Upvotes: 1

user3527357
user3527357

Reputation:

The problem is that the function void confirmDimension(int dimension, char what) takes a char as its second parameter, but you are passing, for example 'ht', which is two bytes and is losing data when being converted to a char, causing the code if (what == 'ht') to never evaluate to true.

Either make sure you pass a char to confirmDimension() or change it to use a string

Upvotes: 1

Related Questions