Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e8/solution/p4.c

#include <stdio.h>
#include <string.h>
#include <curses.h>

/**
 * Re-use the getint() function from p2.c here:
 */
int getint(int y, int x, char *prompt)
{
  int n, len = strlen(prompt);
  mvprintw(y, x-len, prompt);
  scanw("%d", &n);
  return n;
}

/**
 * Prompt for an X, Y, Width and Height integers using the getint() function,
 * then clear the screen and draw a hollow box composed of hash marks (#) who's
 * upper left corner starts at the x,y position input and is width and height
 * in size.  Then wait for a key-press to exit the program.
 */
int main(void)
{
  /* Do not modify the following code: */
  initscr();
  clear();

  int x = getint(10, 10, "X? ");
  int y = getint(11, 10, "Y? ");
  int w = getint(12, 10, "Width? ");
  int h = getint(13, 10, "Height? ");

/**
 * Clear the screen and draw a hollow box composed of hash marks (#) where the
 * upper left corner starts at the x,y position input and is w characters wide
 * (its width) and h characters high (its height) in size.  Then wait for a
 * key-press to exit the program.
 */
  clear();

  for(int c=0; c < w; c++) {
    mvaddch(y    ,x+c,'#');
    mvaddch(y+h-1,x+c,'#');
  }
  for(int r=0; r < h; r++) {
    mvaddch(y+r,x  ,'#');
    mvaddch(y+r,x+w-1,'#');
  }

  mvprintw(LINES-1, 0, "Press any key to exit");
  refresh();

  getch();
  endwin();
  return 0;
}