C++ cin Letter-guessing game

Description

C++ cin Letter-guessing game

#include <iostream>
using namespace std;
int main()/*from   w w  w . ja v a2s  . co  m*/
{
   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;
}



PreviousNext

Related