无法恢复到While循环的顶部
Unable to revert back to top of While loop
我是一个c++新手,有一个关于我正在研究的问题。
我正在编写的程序被设计成做以下事情:
- 要求用户输入三角形的3条边的长度
- 检查三条边是否通过三角形不等式定理(和)的值必须大于第三边)
- 计算三角形的面积和周长
**在步骤2中,如果用户输入的边长违反三角形不等式定理,则要求用户再次输入边长
例如,我输入sides:
- : 7
- B: 16
- C: 10
得到所需的输出,并提示输入另一组边长。但是,如果我输入以下内容:
- : 2
- B: 1 1 <<li> C:/gh>
得到:
"三角形的面积为:-1。#印第安纳
三角形的周长为:9""三角不等式定理被违反了。
请再次输入A、B、C边的长度。"
给定边2,1,1,我想输出:
"三角不等式定理被违反了。
请再次输入A、B、C边的长度。"
…并返回到输入边长的提示符
我不知道我的代码哪里出错了,希望有人能帮助我。
下面是我的代码:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void calculate(double& side_a, double& side_b, double& side_c);
void inputLengths(double& side_a, double& side_b, double& side_c);
bool isValid(double& side_a, double& side_b, double& side_c);
int _tmain(int argc, _TCHAR* argv[])
{
double side_a = 3;
double side_b = 3;
double side_c = 3;
while (isValid(side_a, side_b, side_c) == true)
{
inputLengths(side_a, side_b, side_c);
calculate(side_a, side_b, side_c);
if (isValid(side_a, side_b, side_c) == false)
{
cout << "n";
cout << "The triangle inequality theorem has been violated." << endl;
cout << "Please enter the lengths of side A, side B and side C again." << endl;
continue;
}
}
return 0 ;
}
void calculate(double& side_a, double& side_b, double& side_c)
{
double perimeter = side_a + side_b + side_c ;
double semiperimeter = (side_a + side_b + side_c) / 2 ;
double area = sqrt(semiperimeter * (semiperimeter - side_a) * (semiperimeter - side_b) * (semiperimeter - side_c));
cout << "n";
cout << "Given a triangle with the following side lengths:" << endl;
cout << "n";
cout << "Side A: " << side_a << endl;
cout << "Side B: " << side_b << endl;
cout << "Side C: " << side_c << endl;
cout << "n";
cout << "The area of the triangle is: " << area << endl;
cout << "The perimeter of the triangle is: " << perimeter;
cout << "n";
}
void inputLengths(double& side_a, double& side_b, double& side_c)
{
cout << "n";
cout << "Please enter the length of side A: ";
cin >> side_a;
cout << "Please enter the length of side B: ";
cin >> side_b;
cout << "Please enter the length of side C: ";
cin >> side_c;
}
bool isValid(double& side_a, double& side_b, double& side_c)
{
// use the triangle inequality theorem to test that the sum of any two sides is
greater than the third side
if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
side_a))
{
return true;
}
else
{
return false;
}
}
设valid
变量代表isValid(side_a, side_b, side_c)
,其中只能在一个位置更新状态。
valid = true;
while (valid == true) // <-- `continue`s here, checking condition
{
valid = updateState(); // i.e. dependent upon `inputLengths`
if (valid == false)
{
continue;
}
/* implicitly */ continue;
}
这显然代表了一个问题,因为代码说"当无效时继续(或者,总是隐式地),但是只在有效时循环"。
也就是说,在无效的情况下,循环在被告知"继续"后立即结束——可以改变有效性的代码不有机会再次执行
相反,请考虑以下内容:
// Loop repeatedly until the user enters a "valid input" state
valid = false;
while (true) {
valid = readTriangleSides(); // Ask for the input; is it valid?
if (!valid) {
// Inform the user the input was invalid; implicit `continue`
} else {
// Valid, end the loop - this could be eliminated if the
// loop was expressed as `while (!valid) ..`
break;
}
}
// Use the obtained values, now in the "valid input" state
calculateAndDisplay();
A=2,B=1,C=1是一条长度为2的直线(因此是一个简并三角形),在这种情况下A=B+C
当你在函数"isvalid"中测试你的值时:
if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
side_a))
你不认为这种情况是简并情况(其中side_b+side_c=side_a)。因此,必须用">="替换所有出现的">",如下所示:
if ((side_a + side_b >= side_c) && (side_a + side_c >= side_b) && (side_b + side_c >=
side_a))
我看到了自己的错误。我在检查有效性之前进行计算。对calculate()的调用应该在while循环中移到较低的位置。并且,应该有一个回调inputlength_()。代码应该是:
int _tmain(int argc, _TCHAR* argv[])
{
double side_a = 3;
double side_b = 3;
double side_c = 3;
while (isValid(side_a, side_b, side_c) == true)
{
inputLengths(side_a, side_b, side_c);
if (isValid(side_a, side_b, side_c) == false)
{
cout << "n";
cout << "The triangle inequality theorem has been violated." <<
endl;
cout << "Please enter the lengths of side A, side B and side C
again." << endl;
inputLengths(side_a, side_b, side_c);
}
calculate(side_a, side_b, side_c);
}
return 0 ;
}
感谢所有人的慷慨帮助。
,以及c .
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 如何在不使用循环的情况下回到代码的顶部?
- 让 while 循环回顶部
- 如何在C++中循环到顶部?你能给我举个例子吗?
- 无法恢复到While循环的顶部
- 无限循环 - 顶部或底部