输出流中出现的尚未输入的C++数字
C++ numbers appearing in output stream that have not been input
我在输出流中收到奇怪的数字,这些数字出现在输出的文本中(在控制台中)。无论我输入什么数字,数字似乎都以相同的顺序出现。它们分别为 0、80、0。我的代码下面是一个示例输出。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
//write program so that a<=b<c or find a way to sort the program so the numbers are in ascending order
cout << "This program uses the input of the lengths of 3 sides of a triangle to determine if the triangle is a right triangle." << endl;
cout << "Enter the length of side 'a'. " << a << "n";
cin >> a;
cout << "Enter the length of side 'b'. " << b << "n";
cin >> b;
cout << "Enter the length of side 'c'. " << c << "n";
cin >> c;
if ((a * a) + (b * b) == (c * c)) // This means (a^2)+(b^2)=(c^2)
{
cout << "This is a right triangle." << "n";
}
else if ((b * b) + (c * c) == (a * a))
{
cout << "This is a right triangle." << "n";
}
else if ((a * a) + (c * c) == (b * b))
{
cout << "This is a right triangle." << "n";
}
else
{
cout << "This is not a right triangle." << "n";
}
return 0;
}
该程序使用三角形 3 条边的长度的输入来确定三角形是否为直角三角形。输入边"a"的长度。02输入边"b"的长度。803输入边"c"的长度。05这不是直角三角形。
in
cout << "Enter the length of side 'a'. " << a << "n";
<< a
指示程序打印 a
的当前值。此时a
没有定义的值,但变量存在,因此尝试打印它在语法上是正确的并且可以编译。使用这种未初始化的a
会导致未定义的行为,因此任何事情都可能发生,但最有可能发生的是现在被a
占用的内存中碰巧被打印的任何垃圾。在您的情况下,结果为 0。
解决方案是不要打印出a
的值。似乎没有必要这样做。
cout << "Enter the length of side 'a'.n";
对侧 b 和 c 重复此操作。
在读取未初始化的变量之前,a
、b
和 c
打印它们。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type