我正在研究一个问题,我有一个函数可以检查矩形块中球形和圆柱形孔的数量是否大于零。在这个问题上,我必须调用函数两次。我的问题是,第一次调用函数“ HoleCheck”运行两次,并假设为圆柱孔输入了零,并使我重新输入了球形孔的值。我怎样才能只进行一次球形孔检查?

#include <iostream>
#include <string>

using namespace std;

void Check(double arr1[], string arr2[]);
void HoleCheck(int arr3[], string arr4[]);


int main()
{

double DimArray[3];
string MyArray[3] = { "Height", "Length", "Width"};
int totalholes[2];
string holes[2] = { "Spherical", "cylindrical"};

cout << "Enter the height, length and width of rectangle in centimeters: ";
cin >> DimArray[0] >> DimArray[1] >> DimArray[2];
Check(DimArray, MyArray);

cout << "How many spherical bubbles are present? ";
cin >> totalholes[0];
HoleCheck(totalholes, holes);
cout << "How many cylindrical holes are present? ";
cin >> totalholes[1];
HoleCheck(totalholes, holes);

return 0;
}

void Check(double arr1[], string arr2[])
{
int i;
for (i = 0; i < 3; i++)
{
    while (arr1[i] <= 0)
    {
        cout << "Your entered " << arr2[i] << " is less than zero!\n";
        cout << "Please re-enter a valid number --> ";
        cin >> arr1[i];
    }
}
}
void Check(double arr1[], string arr2[])
{
int z;
for (z = 0; z < 2; z++)
{
    while (arr3[z] <= 0)
    {
cout << "The number of " << arr4[z] << " holes must be greater than 0.\n";
        cout << "Please re-enter a valid number --> ";
        cin >> arr3[z];
    }
}
}

最佳答案

假设您的第二个Check函数实际上是HoleCheck,这是一个错字:

您的HoleCheck函数检查其arr3的两个元素,但是您在将任何值输入totalHoles[1]之前先调用它。

只需删除对HoleCheck的第一个调用或进行更改,就可以告诉它要检查数组中的哪个条目。

10-05 22:42