fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4. #include <algorithm>
  5.  
  6. std::string generateRandomCode(int length = 8) {
  7. const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  8. std::random_device rd;
  9. std::mt19937 generator(rd());
  10. std::uniform_int_distribution<> distribution(0, characters.size() - 1);
  11.  
  12. std::string code;
  13. for (int i = 0; i < length; ++i) {
  14. code += characters[distribution(generator)];
  15. }
  16. return code;
  17. }
  18.  
  19. int main() {
  20. std::string username;
  21. std::cout << "กรุณาใส่ชื่อผู้ใช้: ";
  22. std::getline(std::cin, username);
  23.  
  24. std::string code = generateRandomCode();
  25. std::cout << "ยินดีต้อนรับ " << username << "!" << std::endl;
  26. std::cout << "รหัสของคุณคือ: " << code << std::endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.03s 25848KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <string>
#include <random>
#include <algorithm>

std::string generateRandomCode(int length = 8) {
    const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_int_distribution<> distribution(0, characters.size() - 1);

    std::string code;
    for (int i = 0; i < length; ++i) {
        code += characters[distribution(generator)];
    }
    return code;
}

int main() {
    std::string username;
    std::cout << "กรุณาใส่ชื่อผู้ใช้: ";
    std::getline(std::cin, username);

    std::string code = generateRandomCode();
    std::cout << "ยินดีต้อนรับ " << username << "!" << std::endl;
    std::cout << "รหัสของคุณคือ: " << code << std::endl;

    return 0;
}