I want to list all the numbers from 0000-9999, but I have problems with zero places.
I tried:
for(int i = 0; i <= 9999; ++i) { cout << i << "\n"; }
but I get: 1,2,3,4..ect how can I do this 0001 0002 0003 .... 0010 etc.
See setfill for a character fill and setw for a minimum width.
Your case will look like this:
for(int i = 0; i <= 9999; ++i) { cout << setfill('0') << setw(4) << i << "\n"; }
You just need to set some flags:
#include <iostream> #include <iomanip> using namespace std; int main() { cout << setfill('0'); for(int i = 999; i >= 0; --i) { cout << setw(4) << i << "\n"; } return 0; }
Use ios_base::width() and ios::fill() :
ios_base::width()
ios::fill()
cout.width(5); cout.fill('0'); cout << i << endl;
Alternatively, use the IO manipulators:
#include<iomanip> // ... cout << setw(5) << setfill('0') << i << endl;
Although this is not required, but if you want to know how to do this with C, here is an example:
for (int i = 0; i <= 9999; i++) printf("%04d\n", i);
Here, "0" in "% 04d" works like setfill('0') , and "4" works like setw(4) .
setfill('0')
setw(4)
Source: https://habr.com/ru/post/1310712/More articles:ASP.Net MVC Custom Controls - Container - asp.netWhat is the reason for GeneratedValue jumps (strategy = GenerationType.TABLE) when @TableGenerator is not specified? - javaSilverlight list with vertical highlight effect required - c #Progress bar on applet loading - javaHow expensive are variables in JavaScript? - performanceHibernate parent version control - javajquery dynamic id - javascriptSharing features of an MVC application - c #How to write a list to a file? - listUnderline as segment_separators in routing.yml - symfony1All Articles