Download as pdf
Download as pdf
You are on page 1of 1
Computer Project (prepared by M.Hassan Arshad) 1. include : Includes the input/output stream library, allowing input and output operations. 2. #include : Includes the string library, providing string manipulation functionalities. 3. #include : Includes the ctime library, which is used for date and time functions. 4. #include :Includes the cstdlib library, which provides functions for generating pseudo-random numbers. 5. using namespace std;: Declares that symbols defined in the std namespace are being used, avoiding the need to prefix standard library int main() {: Begins the definition of the main function, the entry point of the program. 7. string words[5] = {"apple", "banana", "cherry", "date", “elderberry"};: Declares an array words of strings with 5 elements, each containing a fruit word. 8. srand(time(0));: Seeds the random number generator with the current time, ensuring different random numbers on each program run. 9. int randomindex = rand() % 5;: Generates a random index within the range of the array. 10. string secretWord = words[randomindex];: Assigns a random word from the words array to secretWord. 11. string guessedWord = "";: Initializes an empty string for the guessed word. 12. for (int i = 0; i < secretWord.length(); i++) {: Initiates a loop to create a string with dashes equal to the length of the secretWord. 13. guessedWord + ppends a dash to the guessedWord string in each iteration. 14. int tries = 7;: Initializes the variable tries to 7, representing the number of tries allowed. 15. while (tries > 0 && guessedWord != secretWord) {: Starts a loop that continues as long as there are tries left and the guessed word is not equal to the secret word. 16. char guess;: Declares a variable guess to store the user's guess. 17. cin >> guess;: Reads the user's input (a single character) and stores it in guess. 18. for (int i = 0; i < secretWord.length(); i++) {: Starts a loop to check if the guessed letter matches any character in the secret word. 19. if (secretWordfi iin the secret word. guess) {: Checks if the guessed letter matches the character at index 20. guessedWord[i] = guess;: Updates the guessed word at index i with the guessed letter if it matches. 21. tries--j: Decrements the number of tries if the guess is incorrect 22. cout << "Incorrect guess. Try again.\1 guess. 3: Displays a message indicating an incorrect 23. cout << "Correct guess!\n"5;: Displays a message indicating a correct guess. 24. if (guessedWord == secretWord) {: Checks if the guessed word is equal to the secret word. 25. cout << "Congratulations! You guessed the word: " << secretWord << "\n"; message indicating the player has guessed the word correctly. Displays a 26. cout << "You ran out of tries. The word was: " << secretWord << "\n";: Displays a message indicating the player has run out of tries and reveals the secret word. 27. return 0;: Indicates successful termination of the program

You might also like