函数中的循环运行超出了需要
Loop in function running more than it needs to
在我正在处理的一个问题中,我有一个函数可以检查矩形块中的球形和圆柱形孔的数量是否大于零。在这个问题上,我必须调用函数两次。我的问题是,我第一次调用函数"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
的第一个调用,要么更改它,以便告诉它要检查数组中的哪个条目。
相关文章:
- 循环无限运行C++解决骑士之旅问题
- 我的代码运行良好,但在游戏循环中中断
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 重新启动后,线程无法在 while 循环中再次运行
- 运行无限循环的最小二叉树问题
- 目标C++,如何在后台线程中使用运行循环?
- 如何在指定的时间内运行循环
- 苹果运行循环的回调方法
- 我怎样才能一直运行循环,直到按下键 (C++)
- 如何在C++中每次运行循环时将变量递增1`
- 如何在第一次运行循环时忽略代码的特定部分
- 如何在每次运行循环时更改for循环中的对象
- 应用程序在访问主运行循环时崩溃 (SIGABRT)
- osX :运行循环选项(创建事件点击时)
- 线程如何运行循环直到连接
- 在指定时间运行c++循环
- 在运行循环外声明、分配和释放线程局部指针变量
- 这是在c++中运行循环的有效方式吗?
- 如何在qt中使用gui对象名运行循环