无法获得程序来识别用户是否输入零

时间:2014-04-18 00:54:59

标签: c++

我一直在研究一个问题,我必须制作一个程序,从矩形块的体积中减去球体和圆柱的体积。但是我无法识别用户是否为任何维度输入0。

#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;
}

2 个答案:

答案 0 :(得分:1)

当您输入char时,您正在检查输入是否为string

void confirmDimension(int dim, char what); 应为void confirmDimension(int dim, string what);

然后当你打电话给confirmDimensions时,它应该是

confirmDimensions(height, "ht");

而不是confirmDimensions(height, 'ht');

您应该在confirmDimensions函数中同样更改它。

即。在它应该阅读的功能中 if (what == "ht") 并不是 if (what =='ht')

答案 1 :(得分:1)

问题是函数void confirmDimension(int dimension, char what)char作为其第二个参数,但是您正在传递,例如'ht',这是两个字节并且在转换时丢失数据到char,导致代码if (what == 'ht')永远不会评估为真。

要么确保将char传递给confirmDimension(),要么将其更改为使用string