Advertisement
Guest User

Thing

a guest
Oct 19th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. // Author: Dave Wessels
  2. // File:   lab5.cpp
  3. // Purpose: drawing ascii boxes of a user chosen size in the range 1-80
  4.  
  5. // cstdio used for printf/scanf, cctype used for isspace
  6. #include <cstdio>
  7. #include <cctype>
  8.  
  9. // define the minimum and maximum sizes of the squares
  10. const int MinSize = 1;
  11. const int MaxSize = 80;
  12.  
  13. // define the characters used to print the box components
  14. const char boxTopBot   = '-';
  15. const char boxInternal = ' ';
  16. const char boxSide     = '|';
  17. const char boxCorner   = '+';
  18.  
  19. // prints the outline of a square using | - and + symbols,
  20. //    where size specifies the number of characters per side
  21. void printSquare(int size);
  22.  
  23. // display the program overview and instructions,
  24. //    min/max give the minimum/maximum box sizes
  25. void displayOverview(int min, int max);
  26.  
  27. // gets the user to enter an integer in a specific range
  28. int getInteger(int min, int max);
  29.  
  30. int main()
  31. {
  32.    // display the program intro
  33.    displayOverview(MinSize, MaxSize);
  34.  
  35.    // get the size of the desired square
  36.    int size = getInteger(MinSize, MaxSize);
  37.  
  38.    // print the square
  39.    printSquare(size);
  40.  
  41.    // terminate the program
  42.    printf("\nBye!\n\n");
  43.    return 0;
  44. }
  45.  
  46. // gets the user to enter an integer in a specific range
  47. int getInteger(int min, int max)
  48. {
  49.    int userVal;  // an integer value entered by the user
  50.    int valsRead; // the number of integer values just read
  51.  
  52.    // validData is set to true once a valid integer has been obtained
  53.    bool validData = false;
  54.  
  55.    // keep prompting the user and reading values until
  56.    //    a valid integer is obtained
  57.    do {
  58.       // prompt the user and read their response
  59.       printf("Please enter an integer in the range %d to %d\n", min, max);
  60.       valsRead = scanf("%d", &userVal);
  61.  
  62.       // see if non-integer data was entered
  63.       if (valsRead < 1) {
  64.          // discard the next input field as bad input
  65.          // and display an error message
  66.          scanf("%*s");
  67.          printf("***ERROR: that was not an integer, please try again\n\n");
  68.       }
  69.  
  70.       // see if an out-of-range integer was entered
  71.       else if ((userVal < min) || (userVal > max)) {
  72.          // the input was out of bounds,
  73.          // ignore it and display an error message
  74.          printf("***ERROR: that was not an integer in the range %d to %d, ", min, max);
  75.          printf("please try again\n\n");
  76.       }
  77.  
  78.       // a valid integer was entered
  79.       else {
  80.          validData = true;
  81.       }
  82.  
  83.    } while (!validData);  
  84.  
  85.    // return the valid value once obtained
  86.    return userVal;
  87. }
  88.  
  89.  
  90. // display the program overview and instructions,
  91. //    min/max give the minimum/maximum box sizes
  92. void displayOverview(int min, int max)
  93. {
  94.    printf("This program draws an ascii square of a user-chosen size\n");
  95.    printf("The size must be an integer in the range %d..%d\n\n", min, max);
  96. }
  97.  
  98.  
  99. // prints the outline of a square using | - and + symbols,
  100. //    where size specifies the number of characters per side
  101. void printSquare(int size)
  102. {
  103.    // print a header
  104.    printf("\nPRINTING A SQUARE OF SIZE %d: \n\n", size);
  105.  
  106.    // for each row
  107.    //     for each column in that row
  108.    //         if that position is a corner print +
  109.    //         if that position is a top/bottom border print -
  110.    //         if that position is a side border print |
  111.    //         if that position is internal print a space
  112.    //     print a newline after each row is completed
  113.    for (int row = 0; row < size; row++) {
  114.  
  115.        for (int col = 0; col < size; col++) {
  116.  
  117.            // see if it is a top/bottom border (print +)
  118.            if ((row == 0) || (row == (size-1))) {
  119.               // if so, see if it is a corner
  120.               if ((col == 0) || (col == (size-1))) {
  121.                  printf("%c", boxCorner);
  122.               }
  123.               // otherwise print the - for a top/bottom border
  124.               else {
  125.                  printf("%c", boxTopBot);
  126.               }
  127.            }
  128.  
  129.            // if it isn't a top/bottom border,
  130.            //    see if it is a side border (print |)
  131.            else if ((col == 0) || (col == (size-1))) {
  132.               printf("%c", boxSide);
  133.            }
  134.  
  135.            // otherwise it is internal (print a space)
  136.            else {
  137.               printf("%c", boxInternal);
  138.            }
  139.        }
  140.        // print a newline at the end of each row
  141.        printf("\n");
  142.    }
  143.  
  144.    // print an extra newline after the last row
  145.    printf("\n");
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement