简体   繁体   English

c ++数组打印十六进制?

[英]c++ Array printing hex?

hope you guys can help. 希望你们能提供帮助。 When I try add values to this array ie 1,2,3,4 it will print out as a hex number? 当我尝试将值添加到该数组即1,2,3,4时,它将打印为十六进制数字吗? How can I actually print the array non-hex? 我如何才能实际打印非十六进制数组? Thanks 谢谢

int main()
{
    int gr1;
    int gr2;
    int gr3;
    int gr4;
    int addup[4]={gr1, gr2, gr3, gr4};
    cout << "Please enter the first grade";
    cin >> gr1;
    cout << "Please enter the second grade";
    cin >> gr2;
    cout << "Please enter the third grade";
    cin >> gr3;
    cout << "Please enter the fourth grade";
    cin >> gr4;

        cout << addup;

}

You are adding uninitialized variables to your array. 您正在向数组添加未初始化的变量。

int main()
{
    int gr1;
    int gr2;
    int gr3;
    int gr4;
    cout << "Please enter the first grade";
    cin >> gr1;
    cout << "Please enter the second grade";
    cin >> gr2;
    cout << "Please enter the third grade";
    cin >> gr3;
    cout << "Please enter the fourth grade";
    cin >> gr4;

    int addup[4]={gr1, gr2, gr3, gr4};

    for ( int i = 0; i < 4; i++ )
        cout << addup[i];

}

cout << addup prints the memory address, you need a for loop to print out the values: cout << addup打印内存地址,您需要一个for循环来打印出值:

for(int i : addup)
    cout << i << endl;

As variables gr1, gr2, gr3, gr4 were not initialized 由于变量gr1,gr2,gr3,gr4未初始化

int gr1;
int gr2;
int gr3;
int gr4;

array addup has undefined values. 数组加法具有未定义的值。

int addup[4]={gr1, gr2, gr3, gr4};

You should at first assign values to the variables and only after that define the array 首先,您应该为变量分配值,然后再定义数组

int gr1;
int gr2;
int gr3;
int gr4;

cout << "Please enter the first grade";
cin >> gr1;
cout << "Please enter the second grade";
cin >> gr2;
cout << "Please enter the third grade";
cin >> gr3;
cout << "Please enter the fourth grade";
cin >> gr4;

int addup[4]={gr1, gr2, gr3, gr4};

As for this statement 至于这个说法

cout << addup;

then it displays the address of the first element of the array. 然后显示数组第一个元素的地址。 To display the array itself use the following construction 要显示数组本身,请使用以下结构

for ( int x : addup ) cout << x << ' ';
cout << endl;

You can't just format an array: there is no overloaded output operator. 您不能只格式化数组:没有重载的输出运算符。 You can print it like this, though: 您可以像这样打印它:

std::copy(std::begin(addup), std::end(addup),
          std::ostream_iterator<int>(std::cout, " "));

(assuming you use C++11 and include <iterator> and <algorithm> ). (假设您使用C ++ 11并包含<iterator><algorithm> )。 Note that even if you print the value, they won't be the values you expect: the array is initialized at the point it defined using the value of the variables at this point. 请注意,即使您打印该值,它们也不会是您期望的值:数组将在此时使用变量值定义的点处初始化。 Just because you change the variables later doesn't mean it affects the array: the values are copied upon definition, not referenced. 仅仅因为稍后更改变量并不意味着它会影响数组:值是在定义时复制的,而不是被引用的。

Note that you should verify that you should also verify that you actually successfully read the value, eg, using 请注意,您应该验证自己是否也已成功验证了该值,例如,使用

if (std::cin >> gr1 >> gr2 >> gr3 >> gr4) {
    // do something with the input
}

Without checking you may easily process random data: you should always verify that the input was actually successful. 无需检查即可轻松处理随机数据:您应始终验证输入是否实际成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM