mercredi 7 avril 2021

How to add hyphens inside 32 alpha numeric strings

Basically my program randomly generates 32 alpha numeric strings and I want to add hyphens between the characters exactly like this format 56C13783-RR1B-1B66-7685-83C9343667AE. My code before trying to add changes was previously in this format EA25D82041974B9089459223D74E84DA.

#include <iostream>
#include <string>
#include <cstdlib>     /* srand, rand */
#include <ctime>
using namespace std;

string RandomString(int len)
{
   string str = "0123456789ABCDEFabcdef";
   string newstr;
   int pos;
   while(newstr.size() != len) {
    pos = ((rand() % (str.size() - 1)));
    newstr += str.substr(pos,1);
    }
   return newstr;
}

int main()
{
   srand(time(NULL));
   int user_input;
   int hyphens[4]={9,14,19,24};

   
   cout << "Enter how many codes you want: ";
   cin >> user_input;
   for (int i = 0; i < user_input; i++)
   {
   string random_str = RandomString(32);
   cout << "random_str: " << random_str << endl;
  for (int i = 0; i < random_str.length(); ++i) {
 
 
        if (random_str[i] == hyphens) {
 
            random_str[i] = '-';
        } 
  }
}
}

main.cpp: In function ‘int main()’: main.cpp:35:30: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if (random_str[i] == hyphens) {

This was the error I got when trying to compile my code.




Aucun commentaire:

Enregistrer un commentaire