fork download
  1. //Devin Scheu CS1A Chapter 5, P. 297, #22
  2. //
  3. /**************************************************************
  4. *
  5. * DISPLAY A SQUARE OF X CHARACTERS
  6. * ____________________________________________________________
  7. * This program creates a square pattern using X characters
  8. * based on a positive integer side length.
  9. *
  10. * Computation is based on nested loops to print X characters
  11. * for each row and column
  12. * ____________________________________________________________
  13. * INPUT
  14. * sideLength : The length of each side of the square
  15. *
  16. * OUTPUT
  17. * squarePattern : The square pattern of X characters
  18. *
  19. **************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23.  
  24. using namespace std;
  25.  
  26. int main () {
  27.  
  28. //Variable Declarations
  29. int sideLength; //INPUT - The length of each side of the square
  30. string squarePattern; //OUTPUT - The square pattern of X characters
  31.  
  32. //Prompt for Input
  33. cout << "Enter a positive integer no greater than 15: \n";
  34. cin >> sideLength;
  35.  
  36. //Input Validation
  37. while (sideLength < 1 || sideLength > 15) {
  38. cout << "\nError: Please enter a number between 1 and 15: \n";
  39. cin >> sideLength;
  40. }
  41.  
  42. //Display Square Pattern
  43. for (int row = 0; row < sideLength; row++) {
  44. for (int col = 0; col < sideLength; col++) {
  45. cout << "X";
  46. }
  47. cout << endl;
  48. }
  49.  
  50. return 0;
  51. } //end of main()
Success #stdin #stdout 0.01s 5320KB
stdin
7
stdout
Enter a positive integer no greater than 15: 
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX