mercredi 7 avril 2021

How to create program that outputs random 32 alpha numeric chars

,I have a program where I need to create 32 alpha numeric characters similar to guids. The goal is to use random number generator and generate the number of strings the user inputs into the program. If the user types in 5, then we get 5 of those random strings. It is also supposed to give the user a choice between using hyphens/no hyphens and upper/lowercase. This is what I have so far, but I'm not sure how to achieve the rest of it.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
using namespace std;

char get_guid_char(bool upper);

int main()
{
   char hyphens[32] = {9, 14, 19, 24};
   srand(time(NULL));
   char get_guid_char(bool upper);
   //cout << "Lowercase or upper case?(l/u) ";
   
   if (hyphens == 'h' || hyphens == 'H')
   {
       hyphens[32] = '-'; 
   }
/*   else if (hyphens == 'n' || hyphens == 'N')
   {
       hyphens[32] = "";
   }*/
    cout << "hyphens or no hyphens? ";
    cin >> hyphens; 

    for (int i = 0, counter = 0; i < 32; i++)
    {
        if ((i == hyphens[counter] - 1))
        {
            counter++;
        }
    }


    return 0;
}



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;   
}



Aucun commentaire:

Enregistrer un commentaire