0

We will modify part of the existing menu logic to generate an ID for the user. This user ID will be of the type int, and will be 5 digits in length. The values are 00001 – 99999, and should appear with all 5 digits (leading zeroes) on the display upon generating them. Do not change the data type from that of an int. Check out the setfill manipulator as a possible helper for this. Setfill allows you to set a fill character to a value using an input parameter, such as ‘0’ for leading zeroes. Researching it, you’ll see that you will need to reset the fill character after use too, or you will be printing a lot of zeroes on the display! Your program also should guarantee an absolutely unique user ID has been generated for each new activity that is added. Think about how this works...

Currently I've been trying to get the following code to work

srand(time(0));
cout << setfill('0') << setw(5) << rand() %99999 << endl;

Problem is that this doesn't seem random at all (it's just slowly counting up based on the computers internal clock right?) and the first digit is always zero. Like the instructions say it should be between 00001 and 99999.

EDIT: I appreciate the different solutions, but most of them are more advanced than what I'm supposed to be using for this assignment. I'm fairly sure srand() and rand() is what I should be using.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Cartino
  • 41
  • 1
  • 7
  • Can't you just have a counter, and increment the counter each time an ID is "generated"? A random solution won't work, since you'll get many collisions. The description doesn't mention it needing to be random. – Carcigenicate Mar 24 '18 at 20:22
  • 2
    Call `srand()` **once** at the start of your program. Then don't call it again. Also, you need to add 1 to `rand() %99999` if you want numbers from 1 to 99999. – r3mainer Mar 24 '18 at 20:23
  • 2
    You should learn about the C++ `random` library. It contains random number generators that are more flexible than `rand()`. – eesiraed Mar 24 '18 at 20:26
  • Stuff your numbers into a `std::set` and they will be unique. – Jesper Juhl Mar 24 '18 at 20:28
  • @squeamishossifrage So I'm calling srand(time(0)) at the beginning and adding 1 onto rand() %99999 but it still seems like it's only generating a four digit number and adding a 0 on the front. – Cartino Mar 24 '18 at 20:31
  • 1
    Don't use `srand` and `rand`. They produce horribly bad random numbers. Use the facilities in `` instead. – Jesper Juhl Mar 24 '18 at 20:31
  • If you must use `rand()` use this: `return (std::rand() % (100000));` and at least consider seeding with `std::random_device seed; srand(seed());` – Carl Mar 24 '18 at 20:40
  • @Carl Don't think I'm allowed to seed that way either. I'm still having issues with it only generating a four digit number this way. Is it maybe just that rand() is really this limited? – Cartino Mar 24 '18 at 20:47

1 Answers1

1

Okay, so it seems you must use the rand() function to generate a value between 1-99999. So with that in mind, the following code should generate random values in the required range:

#include <iomanip>
#include <iostream>

const int randID()
{
    return 1 + (std::rand() % (99999));
}

int main()
{   
    for(int i = 0; i < 1000; ++i)
        std::cout << std::setfill('0') << std::setw(5) << randID() << '\n';

    return 0;
}

For me it prints, for example:

01886
21975
01072
11334
22868
26154
14296
32169
20826
09677
15630
28651

Which should satisfy your requirement of 0-padded values between 1-99999. Also, as mentioned in the comments. Do look into the <random> for your random number needs outside this assignment as it generates far superior random numbers, and offers way more generators, distributions and better seeding.

Carl
  • 2,057
  • 1
  • 15
  • 20