Thursday, March 3, 2022

Guess a Number in C++

 A simple guess a number program written in C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.


My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

Thank you very much for your support.





Program Listing

#include <iostream>
#include <ctime>

const int MaxGuesses = 10;

int main()
{
     int num_guesses = 0, input = 0;
     bool found = false;

     srand(time(nullptr));
     int number_to_guess = (rand() % (999 - 100 + 1)) + 100;
     std::cout << "Number to guess: " << number_to_guess << "\n";
  std::cout << "\n\n";
      std::cout << "\tGuess a Number in C++ ";
      std::cout << "\n\n";
     while (num_guesses < MaxGuesses && !found)
     {

         std::cout << "Enter number between 100 and 999: ";
         std::cin >> input;
         num_guesses++;
         // maybe do some input validation here
         if(input == number_to_guess)
         {
             found = true;
             std::cout << "You found it!\n";
             break;
         }
     }
     if(!found) // only wrong guesses
         std::cout << "Sorry, you didn't guess it.\n";
}

No comments:

Post a Comment