Input routine to ensure user types a correct response. Using do-while Loop - C++ Statement

C++ examples for Statement:do while

Introduction

The format of the do-while is

do
{
   block of one or more C++ statements;
} while (test expression)

Input routine to ensure user types a correct response.

Demo Code


#include <iostream>
using namespace std;
int main()/*  www  .j  a  v  a  2  s  . co m*/
{
   char ans;
   do
   {
      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.
   while ((ans != 'Y') && (ans != 'N'));
   return 0;
}

Result


Related Tutorials