简体   繁体   English

循环运行的功能超出了所需

[英]Loop in function running more than it needs to

In a problem i am working on i have a function that checks if the number of spherical and cylindrical holes in rectangular block are greater than zero. 我正在解决一个问题,我有一个函数可以检查矩形块中球形和圆柱形孔的数量是否大于零。 In this problem i have to call the function twice. 在这个问题上,我必须调用函数两次。 My problem is that the first time i call the function 'HoleCheck' is is running twice and assuming that zero is entered in for the cylindrical holes and makes me re-enter the value for spherical holes. 我的问题是,第一次调用函数“ HoleCheck”运行两次,并假设为圆柱孔输入了零,并使我重新输入了球形孔的值。 How can i make it to only run once for the spherical hole check? 我怎样才能只进行一次球形孔检查?

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

Assuming that your second Check function is actually HoleCheck and that was a typo: 假设您的第二个Check函数实际上是HoleCheck ,这是一个错字:

Your HoleCheck Function checks both elements of its arr3 , but you are calling it before you enter any values into totalHoles[1] . HoleCheck功能检查它的两个元素arr3 ,但你输入任何值到之前,你在呼唤它totalHoles[1]

Either just remove the first call to HoleCheck or change it so you can tell it which entry in the array to check. 只需删除对HoleCheck的第一个调用或对其进行更改,就可以告诉它要检查数组中的哪个条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM