Description
The purpose of this challenge is to manually manipulate character arrays. This challenge creates GUIDs.
What is a GUID?
Here is the wiki article. You’ve probably seen these long, random-looking strings:
{13840352-0cbd-480e-a269-1a5ccf72520c}
{E84BEED6-28CA-407D-98F3-D861536C5A74}
CD46570A-6478-45A8-A2C6-6E93946BCFC6
527A46688EE245669D17478145477D6F
Without getting into the actual technical details of what these strings represent (or that this string really represents a very large number), a visual inspection of a GUID will show that it:
- is comprised of 32 alpha-numeric characters. The only letters used are A-F.
- can have curly braces wrapping it
- can be all upper-case, or all lower-case, but not mixed
- can have hyphen separators or not. If it does have hyphen separators, then they are grouped as such: 8-4-4-4-12
- 12345678-1234-1234-1234-123456789012
Requirements
- There are several methods of creating pure, random GUIDs. In this exercise, we will not use any of those “complicated” methods. Instead, we will simply generate a string, randomly selecting the allowed characters.
- You can use this function to randomly select a single allowed character
char get_guid_char(bool upper) { char result; string allowed = "ABCDEF0123456789"; int index = rand() % allowed.length(); result = upper ? allowed[index] : tolower(allowed[index]); return result; }
- Additionally, we will be saving the generated GUIDS in a file. You can display them on the screen as well, if you choose, but the final output must be in a text file
Partial main()
If you used the suggested function in step 2 above, then you will need to have the following in your .cpp file.
#include <iostream> #include <cstdlib> #include <ctime> int main() { srand(time(NULL)); return 0; }
Sample Interaction / Output
Lower case or upper case: u Dashes or no dashes: n How many GUIDs to create? 10 Done. See GUID.txt $ cat GUID.txt EA25D82041974B9089459223D74E84DA 4DA4166F33584DEB94633AB2AFBF7E47 AA2D938B07994D3883DF1BDDFBFEF95C 21BCFF0340BD4E41986C9FF942D60709 CFE66E1E5E6544309F71AAB8147ADFF4 759C08B84A7748978A4D1FFDA0ECC1A0 D9080C7D8A384F1BBE51B2262BB94D03 9826A697DF424D7E96E20111D32C76C1 41B58FA9961A41829F25EE9CA5C5DA62 DA8955E2B7CD4EB9AD888EFD45512B4F
Sample Interaction / Output
Lower case or upper case: l Dashes or no dashes: d How many GUIDs to create? 5 Done. See GUID.txt $ cat GUID.txt 066cdd0c-d1a9-4e7e-9e33-b111a8ffe5dd 67348423-c471-4f60-ada4-bdef9815a71d 0be69568-0a3b-445b-a5dd-7d880ae60415 3db7041d-254c-4af7-a01a-309f91271f60 159c35a9-a3f8-41ac-82b3-1415090ad6cc
Sample Interaction / Output
Lower case or upper case: u Dashes or no dashes: d How many GUIDs to create? 3 Done. See GUID.txt $ cat GUID.txt 43C72593-EE0C-4C99-8658-83A9436376DF 9E3B28EB-9CC1-4C81-9796-D8AC617854B3 C0EA0387-5372-4DB7-9972-352CA7BD0FFF
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-BB0005