C++ Mini Game: Guess the Letter

Today, I bring you a mini game:Guess the Letter. Compile it quickly and see who is more familiar with the English alphabet! Can the kids understand how it is implemented in C++?

// Note: The file encoding should be GBK#include <iostream>#include <cstdlib>// For rand() and srand()#include <ctime>// For time()// Bit Rabbit - Informatics Competition Productionusing namespace std;int main() {    char secretLetter, guess;    int attempts = 0;// Set random seed    srand(time(0));// Generate a random letter between A and Z    secretLetter = 'A'  + rand() % 26;    cout << "Welcome to the Guess the Letter game!" << endl;    cout << "I have thought of a letter between A and Z, can you guess it?" << endl;    do  {        cout << "Please enter your guessed letter:";        cin >> guess;        attempts++;        // Record the number of attempts        guess = toupper(guess);// Convert letter to uppercase for comparison        if (guess > secretLetter) {            cout << "Too high! Try again."  << endl;        } else if (guess < secretLetter) {            cout << "Too low! Try again."  << endl;        } else  {            cout << "Congratulations, you guessed it right!"  << endl;            cout << "You guessed a total of "  << attempts << " times."  << endl;        }    } while (guess != secretLetter);        return 0;}

Leave a Comment