Ensure user types a correct response. - C++ Language Basics

C++ examples for Language Basics:Console

Description

Ensure user types a correct response.

Demo Code

#include <iostream>
using namespace std;
int main()/*from  www. j a v a 2 s  .co m*/
{
   char ans;
   cout << "Do you want to continue (Y/N)? ";
   cin >> ans;                // Get user's answer
   while ((ans != 'Y') && (ans != 'N'))
   {
      cout << "\nYou must type a Y or an N\n";  // Warn and ask
      cout << "Do you want to continue (Y/N)?"; // again.
      cin >> ans;
   }         // Body of while loop ends here.
   return 0;
}

Result


Related Tutorials