I have made a random password generator.In function void passwordGenerator(int sizeOfPassword)
The problem here is this I am trying to save the password generated by the program in sum
but I don't know how to do it properly.
How do I save random digits password in sum
.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void passwordGenerator(int sizeOfPassword)
{
srand(time(NULL));
char allChars[] = {"0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char sum;
for(int i = 0; i < sizeOfPassword; i++){
sum = sum + allChars[rand()%sizeOfPassword];
}
std::cout<<sum<<std::endl;
}
int main()
{
int sizeOutput;
char wannaPlay = 'y';
while(wannaPlay == 'y'){
std::cout<<"Enter the size of password: ";
std::cin>>sizeOutput;
passwordGenerator(sizeOutput);
std::cout<<"\nRun Again[y/n]? : ";
std::cin>>wannaPlay;
}
return 0;
}
Thank you
You might display letter by letter:
void passwordGenerator(int sizeOfPassword)
{
const char allChars[] = {"0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
for (int i = 0; i < sizeOfPassword; i++){
std::cout << allChars[rand() % sizeof (allChars)];
}
}
or building a string:
std::string passwordGenerator(int sizeOfPassword)
{
const char allChars[] = {"0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
std::string password;
for (int i = 0; i < sizeOfPassword; i++){
password += allChars[rand() % sizeof (allChars)];
}
return password;
}