Merhaba,
C++ ile rastgele parola üreten static bir class yazdım. Fakat bir sorunla karşılaştım. İnternette biraz aradım, fakat çözüme ulaşamadım. Yardımcı olabilirseniz sevinirim.
Kod:
Hata:
İnternette bulduğum çözüm önerilerinide
Bende kodu
Bu hatayı nasıl çözebilirim?
Genel Bilgiler:
Sistem: Windows 10 x64
C++ Versiyonu: C++20
Derleyici: x64 MSVC v19.38
C++ ile rastgele parola üreten static bir class yazdım. Fakat bir sorunla karşılaştım. İnternette biraz aradım, fakat çözüme ulaşamadım. Yardımcı olabilirseniz sevinirim.
Kod:
C++:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
class RandomPasswordGenerator
{
public:
static std::string GenerateRandomPassword(int PasswordLength, bool IncludeSpecialCharacters)
{
std::string LCASE = "abcdefghijklmnopqrstuvwxyz";
std::string UCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string NUM = "0123456789";
std::string SPEC = "/*-+!#$&?_=@{}";
std::vector<std::vector<char>> charGroups;
if (IncludeSpecialCharacters)
{
charGroups = {
std::vector<char>(LCASE.begin(), LCASE.end()),
std::vector<char>(UCASE.begin(), UCASE.end()),
std::vector<char>(NUM.begin(), NUM.end()),
std::vector<char>(SPEC.begin(), SPEC.end())
};
}
else
{
charGroups = {
std::vector<char>(LCASE.begin(), LCASE.end()),
std::vector<char>(UCASE.begin(), UCASE.end()),
std::vector<char>(NUM.begin(), NUM.end())
};
}
std::vector<int> charsLeftInGroup(charGroups.size());
std::iota(charsLeftInGroup.begin(), charsLeftInGroup.end(), 0);
std::vector<int> leftGroupsOrder(charsLeftInGroup);
std::random_device rd;
std::mt19937 rng(rd());
std::shuffle(leftGroupsOrder.begin(), leftGroupsOrder.end(), rng);
std::vector<char> password(PasswordLength);
int nextCharIdx, nextGroupIdx, nextLeftGroupsOrderIdx, lastCharIdx, lastLeftGroupsOrderIdx = static_cast<int>(leftGroupsOrder.size()) - 1;
for (int i = 0; i < PasswordLength; i++)
{
if (lastLeftGroupsOrderIdx == 0)
{
nextLeftGroupsOrderIdx = 0;
}
else
{
nextLeftGroupsOrderIdx = rng() % lastLeftGroupsOrderIdx;
}
nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];
lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;
if (lastCharIdx == 0)
{
nextCharIdx = 0;
}
else
{
nextCharIdx = rng() % (lastCharIdx +1); // hatanın oluştuğu satır
}
password[i] = charGroups[nextGroupIdx][nextCharIdx];
if (lastCharIdx == 0)
{
charsLeftInGroup[nextGroupIdx] = static_cast<int>(charGroups[nextGroupIdx].size());
}
else
{
if (lastCharIdx != nextCharIdx)
{
std::swap(charGroups[nextGroupIdx][nextCharIdx], charGroups[nextGroupIdx][lastCharIdx]);
}
charsLeftInGroup[nextGroupIdx]--;
}
if (lastLeftGroupsOrderIdx == 0)
{
lastLeftGroupsOrderIdx = static_cast<int>(leftGroupsOrder.size()) - 1;
}
else
{
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{
std::swap(leftGroupsOrder[nextLeftGroupsOrderIdx], leftGroupsOrder[lastLeftGroupsOrderIdx]);
}
lastLeftGroupsOrderIdx--;
}
}
return std::string(password.begin(), password.end());
}
};
int main()
{
std::string password = RandomPasswordGenerator::GenerateRandomPassword(12, false);
std::cout << "Generated Password: " << password << std::endl;
std::cin.get();
return 0;
}
Hata:
Exception thrown at 0x00007FF78A655A4E in App1.exe: 0xC0000094: Integer division by zero.
Unhandled exception at 0x00007FF78A655A4E in App1.exe: 0xC0000094: Integer division by zero.
İnternette bulduğum çözüm önerilerinide
nextCharIdx = rng() % (lastCharIdx +1);
kodundaki lastCharIdx
+1 bölümünü silmem söyleniyordu. Yani nextCharIdx = rng() % (lastCharIdx);
olarak değiştirmem gerektiği.Bende kodu
nextCharIdx = rng() % (lastCharIdx);
olarak değiştirim. Derlemede bir hata oluşmadı. Fakat programı çalıştırdığım zaman Runtime Error verdi.Bu hatayı nasıl çözebilirim?
Genel Bilgiler:
Sistem: Windows 10 x64
C++ Versiyonu: C++20
Derleyici: x64 MSVC v19.38