user1810514
user1810514

Reputation: 47

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

Upvotes: 0

Views: 38

Answers (1)

The Dark
The Dark

Reputation: 8514

Assuming that your second Check function is actually HoleCheck and that was a typo:

Your HoleCheck Function checks both elements of its arr3, but you are calling it before you enter any values into 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.

Upvotes: 1

Related Questions