how to put array values in one variable ?

dears

i have the following array:

short int a[4]= {0,2,3,4};

i want to represent it in a variable like:

short int b=0234

how to do it please in c++?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    short int a[4] = {0,2,3,4};
    
    short int b = 0;
    for (int i=0; i<4; ++i)
    {
        b *= 10;
        b += a[i];
    }
    
    // default display
    cout << "b = " << b << '\n';
    
    //display with leading zeros
    cout << setfill('0');
    cout << "b = " << setw(4) << b << '\n';
}
b = 234
b = 0234
thank you so much Chervil
ASIDE

Careful about short int b=0234

C++ treats any integer literal which starts with a 0 as octal.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
  int b = 0234;
  cout << b << "\n";
  return 0;
}


Output is 156 not 234

Andy
Topic archived. No new replies allowed.