fork download
  1. #include <iostream>
  2. //Andres Guzman CSC5 Chapter 5, P. 298, #22
  3. //
  4. /*******************************************************************************
  5. *
  6. * DISPLAY A SQUARE
  7. * ______________________________________________________________________________
  8. * This program will display a square of X
  9. *
  10. * Computation:
  11. *
  12. * Input Validation: num < 1 || num > max
  13. * Dimensions: row/col < num
  14. * ______________________________________________________________________________
  15. * INPUT
  16. *
  17. * num = Allows user to input a value into the program
  18. * OUTPUT
  19. *
  20. * X = Character used to display the shape of the square
  21. *
  22. *******************************************************************************/
  23. #include <iostream>
  24. using namespace std;
  25. int main ()
  26. {
  27. int max = 15; //Max number allowed to be input
  28. int num; //Allows user to input a value into the variable
  29. //
  30. //Display input prompt
  31. cout << "Enter a number no greater than 15: ";
  32. cin >> num;
  33. cout << endl;
  34. //
  35. //Initializing While Loop
  36. while (num < 1 || num > max) //Limits the range of inputs between 1 & 15
  37. {
  38. cout << "\nInvalid number, please try again.\n";
  39. cout << "\nEnter a number no greater than 15: ";
  40. cin >> num;
  41. }
  42. //
  43. //Initialzing For Loop
  44. for (int row = 0 ;row < num;row++) //Outputs the row of characters
  45. {
  46. //
  47. //Nesting For Loop Inside For loop
  48. for (int col = 0 ;col < num; col++) //Outputs the column of characters
  49. {
  50. cout << "X";
  51. }
  52. cout << endl;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 5316KB
stdin
4
stdout
Enter a number no greater than 15: 
XXXX
XXXX
XXXX
XXXX