truncating trailing 0

how do i make it so int a = 00001; output will still be 00001 not 1

std::cout << a; // outpus 1, expected output is 00001
Be aware that any number in your source code that begins with a 0 is an octal literal. So 0123 is actually 01238, which is 8310.

To print with extra zeros, use the setfill() and setw() manipulators:

1
2
3
4
5
6
// Change the fill character from ' '.
// Remains changed until you change it to something else.
cout << setfill('0');

// Print a 1 with four leading zeros:
cout << setw(5) << 1 << "\n";

Hope this helps.

[edit]I keep forgetting to say: don't forget to #include <iomanip>
Last edited on
ignore* Duoas answer was much better

how do i make it so int a = 00001; output will still be 00001 not 1

std::cout << a; // outpus 1, expected output is 00001



use setfill and setw

std::cout << setfill('0') << setw(5) << 1;

// output is 00001;

lookup the setfill and setw functions for more information

Last edited on
well, eventually i will need it to be 000100 or 000123
because it will be auto increment
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

int main()
{
   std::cout << std::setfill('0');
   
   for (int i = 1; i < 124; i++)
   {
      std::cout << std::setw(5) << i << "\t";
   }
   
   std::cout << "\n\n";
   
   return 0;
}


00001   00002   00003   00004   00005   00006   00007   00008   00009   00010
00011   00012   00013   00014   00015   00016   00017   00018   00019   00020
00021   00022   00023   00024   00025   00026   00027   00028   00029   00030
00031   00032   00033   00034   00035   00036   00037   00038   00039   00040
00041   00042   00043   00044   00045   00046   00047   00048   00049   00050
00051   00052   00053   00054   00055   00056   00057   00058   00059   00060
00061   00062   00063   00064   00065   00066   00067   00068   00069   00070
00071   00072   00073   00074   00075   00076   00077   00078   00079   00080
00081   00082   00083   00084   00085   00086   00087   00088   00089   00090
00091   00092   00093   00094   00095   00096   00097   00098   00099   00100
00101   00102   00103   00104   00105   00106   00107   00108   00109   00110
00111   00112   00113   00114   00115   00116   00117   00118   00119   00120
00121   00122   00123
im not working in console, im working on qt
Well, your original post makes it clear you are working with the console:
xenoviaquarta wrote:
how do i make it so int a = 00001; output will still be 00001 not 1

std::cout << a; // outpus 1, expected output is 00001

If you want a QString formatted like that, then follow the QString documentation:
http://doc.qt.io/qt-4.8/qstring.html#arg-10

QString s = QString( "%1" ).arg( 123, 5, 10, '0' );

Hope this helps.
im not working in console, im working on qt

Then why did you use std::cout in the example you gave in your OP?

What kind of widget you are printing to in Qt can make a difference on how you handle this. Generally you are using a string, if you use QLineEdit or QLabel. In that case, just append the number to a string of 0s.
@Duoas sorry but thanks ive fixed it :)
Topic archived. No new replies allowed.