In that case you will just have to generate a number 5 times:
1 2 3 4 5 6 7
char code[6]; //use a char array to hold the numbers
code[5] = '\0'; //NULL terminate the C-string
srand(time(NULL));
for (int i=0; i<5; ++i)
code[i] = rand() % 10; //range 0 to 9
int number = rand () % 10000;
This, according to the examples, generates numbers from 0 to 9,999. int number = 10000 + rand () % 90000;
This, according to the examples, generates numbers ... int number = rand () % 100000;
This, according to the examples, generates numbers ...
std::setw and std::fill let you print every number (as text) with at least 5 characters and use prefix characters, say '0' (default is ' ') to make it that wide.
I am writing a program to print customer bills , calculate prices , tax , labour cost etc using functions , each customer is identified bt a 5 digit number and that number should appear on the bill and customers with a customer number that start with 'o' may get a discount . So basically I need them to start from 00001 to 99999
If you're using these numbers for identification purposes only, then just use a regular integer. Then, when you need to display them, simply left-pad them with zeroes as necessary. keskiverto suggested the use of std::setw and std::fill, which I support.
If you're using these numbers for identification purposes only, then just use a regular integer
I'm not sure I agree. If it's for identification purposes only, it would make sense to just use a string, since the number doesn't need to be calculated with. Furthermore, when you store the id-code in a database, you probably want those zeroes to be there.
Anyway, I'm not an expert, just giving my 2 cents.
I disagree that using a string would be better for two reasons (off of the top of my head):
1. When searching, integer comparison is faster than string comparison. In this case, you would have to perform five times as many comparisons (each index is a five-character array).
2. When inserting, a unique index can be created by increasing the current index by one. If a character array is used, this procedure would become needlessly complicated (casting, carries, overflow, etc.).
3. A third reason could possibly be sorting (it is not guaranteed that a lexicographic comparison will yield desired results).
I'm not too proud to admit when I'm wrong - good points :)
Coming to think of it, in a datastore I would probably use an integer id myself, padding it with zeroes if the display requires it. I guess I was thinking more along the lines of a GUID in the OP's case, which is another thing, of course.