fork download
  1. // Lab 8, Working with structs
  2. // Programmer : Maiar Khattab
  3. // Editor(s) used : Code Blocks 13.12
  4. // Compiler(s) used : Code Blocks 13.12
  5.  
  6. #include<iostream>
  7. using std::cout;
  8. using std::endl;
  9.  
  10. #include<cstdlib>
  11.  
  12. //struct def
  13. struct tod
  14. {
  15. int hour;// the hr , 0-23
  16. int minute;// the min, 0-59
  17. int second;//the sec, 0-59
  18. char descr [32];//the description of the time of day
  19.  
  20. };
  21. //void printTod(const tod&);
  22. int main ()
  23. {
  24. cout << "Lab 8, Working With structs\n";
  25. cout << "Programmer: Maiar Khattab\n";
  26. cout << "Editor(s) used: Code Blocks 13.12\n";
  27. cout << "Compiler(s) used: Code Blocks 13.12\n";
  28. cout << "File: " << __FILE__ << endl;
  29. cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
  30.  
  31. tod theTime[] = {{12,0,0, "noon"},
  32. {0,0,0," midnight"},
  33. {6,00,00," supper "},
  34. {11,30,0,"bedtime"}};
  35.  
  36. for(int i; i <5; i++)
  37. {
  38. char descr [32];
  39. cout << theTime[i].descr << " is " << theTime[i].hour << ':'
  40. << theTime[i].minute << ":" << theTime[i].second << endl;
  41. }
  42. }
  43.  
  44.  
Success #stdin #stdout 0s 5288KB
stdin
 #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

// Function to display the welcome message
void displayWelcomeMessage() {
    cout << "************************" << endl;
    cout << " Welcome to the Number Guessing Game! " << endl;
    cout << "************************" << endl;
    cout << "In this game, you have to guess the correct number between 1 and 100." << endl;
    cout << "You will have multiple attempts until you guess the right number." << endl;
    cout << "Good luck!" << endl;
}

// Function to play the game
void playGame() {
    int lowerBound = 1, upperBound = 100;
    int attempts = 0;
    int maxAttempts = 10;
    int targetNumber, userGuess;

    // Seed random number generator
    srand(time(0));

    // Generate a random number between 1 and 100
    targetNumber = rand() % 100 + 1;

    cout << "I have chosen a number between " << lowerBound << " and " << upperBound << ". Try to guess it!" << endl;

    // Start the game loop
    while (attempts < maxAttempts) {cout << "\nAttempt " << attempts + 1 << " of " << maxAttempts << ": Enter your guess: ";
        cin >> userGuess;
        attempts++;

        // Check if the guess is correct
        if (userGuess < targetNumber) {
            cout << "Too low! Try again!" << endl;
        } else if (userGuess > targetNumber) {
            cout << "Too high! Try again!" << endl;
        } else {
            cout << "Congratulations! You guessed the right number " << targetNumber << " in " << attempts << " attempts!" << endl;
            return; // Exit the function when the user wins
        }

        // If user has used all attempts, inform them about the correct number
        if (attempts == maxAttempts) {
            cout << "Sorry, you've used all " << maxAttempts << " attempts. The correct number was " << targetNumber << "." << endl;
        }
    }
}

// Function to display the game over message
void displayGameOver() {
    cout << "\n************************" << endl;
    cout << "Game Over! Thank you for playing!" << endl;
    cout << "************************" << endl;
}

// Main function
int main() {
    char playAgain;

    // Display welcome message
    displayWelcomeMessage();

    do {
        // Play the game
        playGame();// Ask the user if they want to play again
        cout << "\nWould you like to play again? (y/n): ";
        cin >> playAgain;

        // Make sure input is lowercase
        playAgain = tolower(playAgain);

    } while (playAgain == 'y');

    // Display game over message
    displayGameOver();

    return 0;
}
stdout
Lab 8, Working With structs
Programmer: Maiar Khattab
Editor(s) used: Code Blocks 13.12
Compiler(s) used: Code Blocks 13.12
File: prog.cpp
Complied: Feb 12 2025 at 11:21:50

noon is 12:0:0
 midnight is 0:0:0
 supper  is 6:0:0
bedtime is 11:30:0
EI� �L is -177132888:32764:-604407040