MrTurvey
MrTurvey

Reputation: 23

c++ Array printing hex?

hope you guys can help. When I try add values to this array i.e 1,2,3,4 it will print out as a hex number? 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;

}

Upvotes: 0

Views: 1077

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

As variables gr1, gr2, gr3, gr4 were not initialized

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;

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

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>). 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, e.g., 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.

Upvotes: 1

yizzlez
yizzlez

Reputation: 8805

cout << addup prints the memory address, you need a for loop to print out the values:

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

Upvotes: 3

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

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];

}

Upvotes: 3

Related Questions