Letter-guessing game. - C++ Statement

C++ examples for Statement:do while

Description

Letter-guessing game.

Demo Code

#include <iostream>
using namespace std;
int main()/*from   ww  w .j  a v a2s  . c  om*/
{
   int tries = 0;
   char comp_ans, user_guess;

   comp_ans = 'T';                // Change to a different

   cout << "I am thinking of a letter...";
   do
   {
      cout << "What is your guess? ";
      cin >> user_guess;
      tries++;   // Add 1 to the guess-counting variable.
      if (user_guess > comp_ans)
      {
         cout << "Your guess was too high\n";
         cout << "\nTry again...";
      }
      if (user_guess < comp_ans)
      {
         cout << "Your guess was too low\n";
         cout << "\nTry again...";
      }
   } while (user_guess != comp_ans);  // Quit when a match is found.

   cout << "It took you only " << tries << " tries to guess.";
   return 0;
}

Result


Related Tutorials