输出流中出现的C ++编号尚未输入

时间:2017-10-13 03:46:25

标签: c++ compiler-errors numbers syntax-error output

我的输出流中出现了奇怪的数字,出现在输出文本中(在控制台中)。无论我输入什么数字,数字似乎都以相同的顺序出现。它们分别为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边长度的输入来确定三角形是否是直角三角形。 输入边的长度&#39; a&#39;。 0 2 输入边的长度&#39; b&#39;。 80 3 输入边的长度&#39; c&#39;。 0 五 这不是一个直角三角形。

2 个答案:

答案 0 :(得分:1)

cout << "Enter the length of side 'a'. " << a << "\n";

<< a指示程序打印a的当前值。此时a没有定义的值,但变量存在,因此尝试打印它在语法上是正确的并且编译。使用此uninitialized a会产生undefined behaviour,因此任何都可以发生,但最有可能发生的事情是发生的垃圾在a被占用的内存中被打印出来。在你的情况下,结果是0。

解决方法是不打印a的值。似乎没有必要这样做。

cout << "Enter the length of side 'a'.\n";

对边b和c重复此操作。

答案 1 :(得分:0)

在阅读之前,您正在打印未初始化的变量abc