fork download
  1. // Torrez, Elaine CS1A Chapter 5 P. 298, #21
  2.  
  3. /******************************************************************************************
  4.  *
  5.  * Square Display
  6.  *
  7.  * --------------------------------------------------------------------------------
  8.  * This program asks the user for a number between 1 and 15 and then displays a
  9.  * square made of the letter 'X' with sides equal to that number.
  10.  * --------------------------------------------------------------------------------
  11.  *
  12.  * INPUT
  13.  * size : The size of the square (1–15)
  14.  *
  15.  * OUTPUT
  16.  * A square pattern made of the letter 'X'
  17.  *
  18.  *******************************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <limits>
  22. using namespace std;
  23.  
  24. int main ()
  25. {
  26. int size; // INPUT - Size of the square
  27.  
  28. // Prompt user for input
  29. cout << "Enter a number between 1 and 15: ";
  30. while (!(cin >> size) || size < 1 || size > 15)
  31. {
  32. cout << "ERROR: Enter a number between 1 and 15: ";
  33. cin.clear(); // clear error flag
  34. cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard bad input
  35. }
  36.  
  37. cout << endl;
  38.  
  39. // Display the square pattern
  40. for (int row = 0; row < size; ++row)
  41. {
  42. for (int col = 0; col < size; ++col)
  43. {
  44. cout << "X";
  45. }
  46. cout << endl; // move to next line after each row
  47. }
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5316KB
stdin
5 
stdout
Enter a number between 1 and 15: 
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX