为什么我的C++程序会产生无意义的数学
Why is my C++ program generating nonsense math?
我的程序在下面
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
double perimeter = (a + b + c);
int s=0.5*(a + b + c);
cout << "Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should be the longest.";
cin >> a;
cin >> b;
cin >> c;
cout << "Lengths " ;
cout << a;
cout << " " ;
cout << b;
cout << " ";
cout << c;
cout << "The perimeter of the triangle is ";
cout << perimeter ;
cout << "s equals " ;
cout << s;
}
这是输出:
Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should be the longest.
1
2
3
Lengths 1 2 3
The perimeter of the triangle is 32061
s equals 16030
最后我检查了一下,1 + 2 + 3 不等于 32061。 发生了什么,我该如何解决这个问题?
//我猜这是因为我没有正确的标题//(#somethingoranother)?
还是我还没有宣布?但我不知道怎么做。我记得使用 sqrt 作为函数查找过去的根,但没有
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
double area = 0;
double sqrt=0;
cout << "Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should ne the longest.";
cin >> a;
cin >> b;
cin >> c;
int perimeter = (a + b + c);
int s=0.5*(a + b + c);
area = sqrt ( s*(s - a)*(s - b)*(s - c) );
cout << "The perimeter of the triangle is ";
cout << perimeter ;
cout << "." ;
cout << " s equals " ;
cout << s;
cout << ". ";
cout << "Area is ";
cout << area;
}
只是输入"area ="的长公式,但计算机似乎也不喜欢这样
试试下面的代码。perimeter
和s
计算需要在初始化三角形边的值后进行。
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
cout << "Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should be the longest.";
cin >> a;
cin >> b;
cin >> c;
cout << "Lengths " ;
cout << a;
cout << " " ;
cout << b;
cout << " ";
cout << c;
double perimeter = (a + b + c);
int s=0.5*(a + b + c);
cout << "The perimeter of the triangle is ";
cout << perimeter ;
cout << "s equals " ;
cout << s;
}
您正在将未初始化的值相加。在对它们进行数学运算之前,您需要填写 a、b 和 c。现在你这样做的方式是导致随机值加在一起,这就是为什么你会得到无意义的结果。
将计算移动到程序中的适当位置:在你知道输入数字之后,但在打印出来之前:
cout << "Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should be the longest.";
cin >> a;
cin >> b;
cin >> c;
double perimeter = (a + b + c);
int s=0.5*(a + b + c);
cout << "Lengths " ;
...
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- clang整洁10忽略了我的NOLINT命令
- 为什么我的C++程序会产生无意义的数学