Advertisement
TheWhiteFang

Tutorial 2 Section A part c

Dec 17th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. //http://pastebin.com/u/TheWhiteFang
  2. //Tutorial 2 Section A part C
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int value = 0;
  10.     while (1){
  11.         cout << "Enter a number between 1 & 20: ";
  12.  
  13.         while (!(cin >> value)){
  14.             cout << "Ending program. only integer values between 1 and 20 accepted" << endl;
  15.             cin.clear(); //clear internal error flags
  16.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  17.             return 0;
  18.  
  19.         }
  20.         if (value > 0 && value <= 20){
  21.             int i = 0; int j = 0;
  22.  
  23.             for (i = 0; i < value; i++)
  24.             {
  25.                 for (j = 0; j < value; j++)
  26.                 {
  27.                     if (i == 0 || i == (value - 1) || j == 0 || j == (value - 1)) // ||- logical or  //if j is first or last value, print * //if checks for true false in statement
  28.                     {
  29.                         cout << "* ";
  30.                     }
  31.  
  32.                     else //this will happen if it is either not 1st value or last value
  33.                     {
  34.                         cout << "  ";
  35.                     }
  36.  
  37.                 }
  38.  
  39.                 cout << endl;
  40.             }
  41.         }
  42.         else{
  43.             cout << "Ending program. Only integer values between 1 to 20 are accepted ";
  44.             cout << endl; //cout << "\n"
  45.             return 0; //this will end the program if wrong input is given
  46.         }
  47.     }
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement