simple game in C++ called Guess the Number

 This program generates a random number between 1 and 100 (inclusive) and the player has to guess the number. The program will output "Too high" or "Too low" depending on the player's guess and will keep running until the player correctly guesses the number. At the end, the program will output the number of tries it took the player to guess the number.

description 

 

The program starts by including the necessary libraries: iostream for input and output, cstdlib for the random number generator, and ctime for the time seed. The using namespace std; line allows us to use standard library functions without having to prefix them with std::.

The main() function is where the program starts executing. In this function, the random number generator is initialized with the current time using srand(time(0)). This ensures that the generated random number will be different every time the program is run. The rand() % 100 + 1 generates a random number between 1 and 100 (inclusive).

The int guess variable is used to store the player's current guess, and the int numGuesses variable is used to keep track of how many guesses the player has made. The program then outputs a welcome message and prompts the player to guess a number.

The game loop is implemented using a do-while loop. The loop continues until the player correctly guesses the number. Inside the loop, the player's guess is read from the input using cin >> guess and the number of guesses is incremented by 1.

If the player's guess is too low, the program will output "Too low, try again: " and prompt the player to guess again. If the player's guess is too high, the program will output "Too high, try again: " and prompt the player to guess again. If the player's guess is correct, the program will break out of the loop.

You may like these posts

    After the loop, the program will output a congratulations message along with the number of tries it took the player to guess the number. Finally, the program returns 0 to indicate that it has finished executing without any errors.

    code

    #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); int secretNum = rand() % 100 + 1; int guess; int numGuesses = 0; cout << "Welcome to Guess the Number!" << endl; cout `oaicite:{"index":0,"invalid_reason":"Malformed citation << \"Guess a number between 1 and 100: \";\n\n do\n {\n cin >>"}` guess; numGuesses++; if (guess < secretNum) { cout << "Too low, try again: "; } else if (guess > secretNum) { cout << "Too high, try again: "; } } while (guess != secretNum); cout << "Congratulations! You guessed the number in " << numGuesses << " tries." << endl; return 0; }



     

    Commentaires

    1. To insert a code use <i rel="pre">code_here</i>
    2. To insert a quote use <b rel="quote">your_qoute</b>
    3. To insert a picture use <i rel="image">url_image_here</i>
    Tinggalkan komentar sesuai topik tulisan, komentar dengan link aktif tidak akan ditampilkan.
    Admin dan penulis blog mempunyai hak untuk menampilkan, menghapus, menandai spam, pada komentar yang dikirim
    Affordability

    Affordability

    Design must reflect the practical and aesthetic in business but above all... good design must primarily serve people. Build beautiful websites in mere minutes.

    Basic

    Create email templates for yourself.

    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    $5 Per User Per Month

    Enterprise

    Create email templates for yourself.

    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    $10 Per User Per Month
    advertise

    Business

    Create email templates for yourself.

    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    • White Hat SEO
    $19.99 Per User Per Month

    Retina Ready

    Build trust with prospective clients, delight existing customers, and increase the efficiency

    Welcome to

    Best website for Blog App. Business. Digital. Marketing. Service.

    Even in the most uncertain times, Help Scout keeps you connected with customers.

    Frequently asked questions

    01. How can I download the app ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    02. How can I install the app ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    03. How can I upgrade my current plan ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    04. How can I active the app's features ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    05. How can I download the app ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    06. How can I install the app ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    07. How can I upgrade my current plan ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?
    08. How can I active the app's features ?
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Dignissimos sequi placeat distinctio dolor, amet magnam voluptatibus eos ex vero, sunt veritatis esse. Nostrum voluptatum et repudiandae vel sed, explicabo in?

    We tried to make

    Integrations

    Create stunning, effective sales documents with custom-designed theme & template.

    Marketing

    Create stunning, effective sales documents with custom-designed theme & template.

    Dedicated Support

    Create stunning, effective sales documents with custom-designed theme & template.

    Best Features

    Our features will help
    to improve busines

    Sales teams use PandaDoc to improve deal workflow, insights, and speed while delivering an amazing buying experience. Get your documents out the door fast to keep deals.

    Rechercher dans ce blog

    advertise
    advertise
    advertise

    Built in Browsers

    Build trust with prospective clients, delight existing customers, and increase the efficiency

    Extreme Security

    Build trust with prospective clients, delight existing customers, and increase the efficiency

    Clean Design

    Build trust with prospective clients, delight existing customers, and increase the efficiency

    Easy Customize

    Build trust with prospective clients, delight existing customers, and increase the efficiency

    Awesome Design

    Build trust with prospective clients, delight existing customers, and increase the efficiency