2014-04-17 89 views
0

在我正在處理的問題中,我有一個函數,檢查矩形塊中的球形和圓柱形孔的數量是否大於零。在這個問題中,我必須調用兩次函數。我的問題是,我第一次稱爲「孔檢查」功能是運行兩次,並假設爲圓柱形孔輸入零,並使我重新輸入球形孔的值。我怎樣才能讓它只運行一次球形孔檢查?循環功能運行超過需要

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

瞭解如何使用您的調試器** –

回答

1

假設你的第二個Check函數實際上是HoleCheck,這是一個錯字:

HoleCheck功能檢查其arr3的兩個元素,但你輸入任何值到totalHoles[1]之前,你在呼喚它。

要麼只是刪除第一個電話HoleCheck或改變它,所以你可以告訴它檢查數組中的哪個條目。