Simple RNG game

Question:
Why won’t my game work :melting_face:

Repl link:

/*The code you provided seems to be correct and does not contain any errors. However, there are a few improvements that can be made to enhance the functionality and user experience of the program.

1. Add a prompt for the user to enter their name before selecting an animal type. This will personalize the interaction with the program.

2. Validate the user input for the animal index to ensure that it is a valid integer between 1 and 10. You can use a loop to repeatedly prompt the user until they enter a valid input.

3. Use a more robust method to generate random numbers, such as the `<random>` library. This library provides better random number generation algorithms and is recommended over `std::rand()`.

4. Add error handling for cases where the random number generated is 0. Currently, the code assumes that the random number will always be non-zero, which may not be the case.*/

//Here's an updated version of the code with these improvements:

#include <iostream>
#include <random>
#include <string>

int main() {
    // Prompt the user to enter their name
    std::string playerName;
    std::cout << "Enter your name: ";
    std::getline(std::cin, playerName);

    // Seed the random number generator
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(1, 500);
    int randomNumber = dist(gen);

    // Array of animal types
    std::string animals[] = {"Lion", "Buffalo", "Baboon", "Cat", "Dog", "Cow", "Mouse", "Hen", "Eagle", "Zebra"};

    // Player selects an animal type
    int animalIndex;
    std::cout << "Select an animal type (1-10): ";
    while (!(std::cin >> animalIndex) || animalIndex < 1 || animalIndex > 10) {
        std::cout << "Invalid animal selection. Please select a number between 1 and 10: ";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    // Check if the selected animal matches the random number
    std::string selectedAnimal = animals[animalIndex - 1];
    std::cout << "Selected animal: " << selectedAnimal << std::endl;

    if (selectedAnimal == animals[randomNumber % 10]) {
        // Animal selected matches the random number
        int reward = randomNumber * animalIndex;
        std::cout << "Congratulations, " << playerName << "! You won a reward of " << reward << std::endl;
    } else {
        // Animal selected does not match the random number
        std::cout << "Sorry, " << playerName << ", no reward. Better luck next time!" << std::endl;
    }

    return 0;
}

In this updated code, we have added a prompt for the user to enter their name using std::getline() to read the entire line of input. We have also used the <random> library to generate random numbers using the std::random_device and std::mt19937 classes. The user input for the animal index is validated using a loop that repeatedly prompts the user until they enter a valid input. Finally, we have included the player’s name in the output messages to personalize the interaction.

Hi @DUCK1213 , welcome to the forums!
Your code seems to have been corrected by an AI, but could you elaborate what is not working for you?
Are there any error messages?

3 Likes

Did you forget to remove the triple backticks behind? yeah it is removed.

It looks like you’re using ChatGPT or another similar AI to help you. As a general rule, you should never just copy and paste something an AI spits out. Current AI models can hallucinate code, especially modules or packages that don’t exist. Also, AI models are not great for producing long programs, they are more useful when it comes to small portions or simple functionality.

Another issue is that an AI model may misinterpret what you mean and give you code that doesn’t actually do what you want or function how you expect. I suspect that maybe the issue here is that the AI has not given you the code you wanted, because the code you’ve pasted here runs fine without error.

4 Likes