Number generation and variable types

How could I generate numbers from 000,000,000-000,999,999 in this format:


000000000
000000001
000000002
ETC.


I can easily do this without the zero's in front of the digits but the front digits are a must. Is there a certain variable that I can do math.h (cmath) ops on that supports this??
Last edited on
Figured it out. It requires lists and a lot of unnecessary code. There should be a way to do this without all these lines. I'm sure there's a header out there somewhere...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int main()
{
int diglet[6] = { 0, 0, 0, 0, 0, 0 }; //Call Variable, load initial data.
while (1)                             //Skip all this, Apply base 10 math rules to all slots, define when do stop.
{
 diglet[0] = diglet[0] +1;
 if (diglet[0] == 10)
 {
  diglet[0] = 0;
  diglet[1] = diglet[1] +1;
  if (diglet[1] == 10)
 {
  diglet[1] = 0;
  diglet[2] = diglet[2] +1;
  if (diglet[2] == 10)
 {
  diglet[2] = 0;
  diglet[3] = diglet[3] +1;
  if (diglet[3] == 10)
 {
  diglet[3] = 0;
  diglet[4] = diglet[4] +1;
  if (diglet[4] == 10)
 {
  diglet[4] = 0;
  diglet[5] = diglet[5] +1;
  if (diglet[5] == 10)
 {
  diglet[5] = 0;
  diglet[6] = diglet[6] +1;
  if (diglet[6] == 10)                //End.
 {
 goto yoyo;
 }
 }
 }
 }              //Brackets are always fun...
 }
 }
 }
cout<< "000";
cout<< diglet[5];
cout<< diglet[4];
cout<< diglet[3];          //Output
cout<< diglet[2];
cout<< diglet[1];
cout<< diglet[0];
cout<< "\n";
}
yoyo:
cout<< "\nDone.\n";
getch();
}
You could do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream> //std::cout, std::endl
#include <iomanip> //std::setw, std::setfill, std::boolalpha
#include <ctime> //std::time
#include <cstdlib> //std::rand, std::srand

int main()
{
	std::srand(static_cast<unsigned>(std::time(0)));
	unsigned const min = 0u;
	unsigned const max = 999999u;
	unsigned const digits = 9;
	
	bool again = true;
	std::cout << std::setfill('0');
	while(again)
	{
		unsigned result = rand() % (max - min + 1) + min;
		std::cout << "Random number: " << result << std::endl;
		
		std::cout << std::setw(4) << ',';
		if(result > 999 && result < 1000000) //4-6 digits
		{
			std::cout << std::setw(3) << result / 1000;
			result -= result / 1000 * 1000;
		}
		else
			std::cout << std::setw(4);
			
		std::cout << ',' << std::setw(3) << result  << std::endl;
		
		std::cin >> std::boolalpha >> again;
	}
}
Or possibly streaming the number to a string and parsing it accordingly. I am confused on the 0's on the left :P

*edit added a condition to make sure the number is less than 6 digits (even though the prng does that already :P)
Last edited on
This code is for generating numbers in the range 000-999 . you can expand it.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
      for(int a=0; a<=9 ; a++)
           for(int b=0; b<=9 ; b++)
                for(int c=0; c<=9; c++)
                     cout<<a<<b<<c<<endl;
       return 0;
}

keep it simple ;)
Oh yeah I guess I could have generated a bunch of random digits from 0-9 lol. Meh it was 3 am for me :P
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>
#include <cstdint>

int main() 
{
	for(std::uint32_t i = 0; i < 1000000000; ++i)
		std::cout << std::setfill('0') << std::setw(9) << i << std::endl;
}


Edit: too many zeros.
Last edited on
I think he wanted them to have comma's though after every 3 digits. Or I could have completely misread what he was trying to do.
Last edited on
This was exactly what I was looking for, thanks!


1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
      for(int a=0; a<=9 ; a++)
           for(int b=0; b<=9 ; b++)
                for(int c=0; c<=9; c++)
                     cout<<a<<b<<c<<endl;
       return 0;

keep it simple ;)


Why do I keep forgetting the #1 rule of computers?
If that is the case why not just do
1
2
for(int i = 0; i < 1000; ++i)
    std::cout << i << std::endl;
or replace 1000 with max+1.
Um...

1
2
#include <iomanip>
#include <iostream> 
 
cout << setfill( '0' ) << setw( 9 ) << x << "\n";
Topic archived. No new replies allowed.