0

I am writing a code that generates passwords with a length of 10 characters. The requirement for the passwords is a minimum of 2 uppercase letters, 2 digits, one symbol in a random index and the rest will be lowercase letters. The code will ask the user 2 characters and it will generate characters within the given user input. What I am having trouble with is I am not sure how to make it more randomized. Currently, the code prints the uppercase and the lowercase together for example. How would I make it that these characters end up in random indexes of the password?

This is my code:

#include <iostream>
#include<ctime>

using namespace std;

int main(){

    char minInput, maxInput;

    cout<<"Enter a letter (for minimum value)"<< endl;
    cin>>minInput;

    char min = toupper(minInput);

    

    cout<<"Enter a letter (for maximum value)"<< endl;
    cin>>maxInput;

    char max = toupper(maxInput);

    cout<<"Your password is: ";

    srand(time(0));

    for(int i = 0; i < 2; i++){

        char upper = rand() % (max - min + 1) + min;

        cout<<upper;
    }

    for(int i = 0; i < 2; i++){

        char digit = rand() % ((57) - (48) + 1) + (48);

        cout<<digit;
    }



     for(int i = 0; i < 5; i++){

        char lower = rand() % ((max + 32) - (min + 32) + 1) + (min + 32);

        cout<<lower;
    }

     for(int i = 0; i < 5; i++){

        char symbol = rand() % ((47) - (33) + 1) + (33);

        cout<<symbol;
    }


return 0;

}

1 Answers1

0

You can generate a string instead of printing out to the std::out. From there, you can simply randomize your string like this:

How do I create a random alpha-numeric string in C++?

Use std::stringstream to create your initial string.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175