Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Dave Wessels
- // File: lab5.cpp
- // Purpose: drawing ascii boxes of a user chosen size in the range 1-80
- // cstdio used for printf/scanf, cctype used for isspace
- #include <cstdio>
- #include <cctype>
- // define the minimum and maximum sizes of the squares
- const int MinSize = 1;
- const int MaxSize = 80;
- // define the characters used to print the box components
- const char boxTopBot = '-';
- const char boxInternal = ' ';
- const char boxSide = '|';
- const char boxCorner = '+';
- // prints the outline of a square using | - and + symbols,
- // where size specifies the number of characters per side
- void printSquare(int size);
- // display the program overview and instructions,
- // min/max give the minimum/maximum box sizes
- void displayOverview(int min, int max);
- // gets the user to enter an integer in a specific range
- int getInteger(int min, int max);
- int main()
- {
- // display the program intro
- displayOverview(MinSize, MaxSize);
- // get the size of the desired square
- int size = getInteger(MinSize, MaxSize);
- // print the square
- printSquare(size);
- // terminate the program
- printf("\nBye!\n\n");
- return 0;
- }
- // gets the user to enter an integer in a specific range
- int getInteger(int min, int max)
- {
- int userVal; // an integer value entered by the user
- int valsRead; // the number of integer values just read
- // validData is set to true once a valid integer has been obtained
- bool validData = false;
- // keep prompting the user and reading values until
- // a valid integer is obtained
- do {
- // prompt the user and read their response
- printf("Please enter an integer in the range %d to %d\n", min, max);
- valsRead = scanf("%d", &userVal);
- // see if non-integer data was entered
- if (valsRead < 1) {
- // discard the next input field as bad input
- // and display an error message
- scanf("%*s");
- printf("***ERROR: that was not an integer, please try again\n\n");
- }
- // see if an out-of-range integer was entered
- else if ((userVal < min) || (userVal > max)) {
- // the input was out of bounds,
- // ignore it and display an error message
- printf("***ERROR: that was not an integer in the range %d to %d, ", min, max);
- printf("please try again\n\n");
- }
- // a valid integer was entered
- else {
- validData = true;
- }
- } while (!validData);
- // return the valid value once obtained
- return userVal;
- }
- // display the program overview and instructions,
- // min/max give the minimum/maximum box sizes
- void displayOverview(int min, int max)
- {
- printf("This program draws an ascii square of a user-chosen size\n");
- printf("The size must be an integer in the range %d..%d\n\n", min, max);
- }
- // prints the outline of a square using | - and + symbols,
- // where size specifies the number of characters per side
- void printSquare(int size)
- {
- // print a header
- printf("\nPRINTING A SQUARE OF SIZE %d: \n\n", size);
- // for each row
- // for each column in that row
- // if that position is a corner print +
- // if that position is a top/bottom border print -
- // if that position is a side border print |
- // if that position is internal print a space
- // print a newline after each row is completed
- for (int row = 0; row < size; row++) {
- for (int col = 0; col < size; col++) {
- // see if it is a top/bottom border (print +)
- if ((row == 0) || (row == (size-1))) {
- // if so, see if it is a corner
- if ((col == 0) || (col == (size-1))) {
- printf("%c", boxCorner);
- }
- // otherwise print the - for a top/bottom border
- else {
- printf("%c", boxTopBot);
- }
- }
- // if it isn't a top/bottom border,
- // see if it is a side border (print |)
- else if ((col == 0) || (col == (size-1))) {
- printf("%c", boxSide);
- }
- // otherwise it is internal (print a space)
- else {
- printf("%c", boxInternal);
- }
- }
- // print a newline at the end of each row
- printf("\n");
- }
- // print an extra newline after the last row
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement