C++ examples for Language Basics:Console
Ensure user types a correct response.
#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; }