I want to list all numbers from 0000-9999 however I am having trouble holding the 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 make it 0001,0002,0003....0010, etc
See setfill for specifying the fill character, and setw for specifying the minimum width.
Your case would look like:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With