C++ for statement running at the user's request. exit for loop based on user input

Description

C++ for statement running at the user's request. exit for loop based on user input

#include <iostream>
using namespace std;
int main()/*from w  ww . j a va2 s .  c om*/
{
   int num;   // Loop counter variable
   char ans;
   cout << "Here are the numbers from 1 to 20\n";
   for (num=1; num<=20; num++)
   {
      cout << num << "\n";
      cout << "Do you want to see another (Y/N)? ";
      cin >> ans;
      if ((ans == 'N') || (ans == 'n'))
      {
         break;
      }    // Will exit the for loop if user wants to.
   }
   cout << "\nThat's all, folks!\n";
   return 0;
}



PreviousNext

Related