Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e8/solution/p2.c

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

/**
 * Complete this function to print a prompt where the right edge of the prompt
 * ends up at the x,y location, then use scanw() to get an integer and return
 * the read integer as this functions value.
 * ex:
 *    Prompt? 
 *           ^ x,y should be here.
 * Use the strlen() function to get the length of the prompt string.
 * NOTE: This function should do these things, the program is incomplete if it
 * does not.
 */
int getint(int y, int x, char prompt[])
{
  int n, len = strlen(prompt);
  mvprintw(y, x-len, prompt);
  scanw("%d", &n);
  return n;
}

/**
 * Complete the main function to read two integers using the getint() function,
 * prompting for an X and Y value.  Clear the screen and print a "Hello, world!"
 * message at the location the user input.  Then wait for the user to press
 * a key to quit the program.
 */
int main(void)
{
  initscr();

  int x = getint(10, 10, "X? ");
  int y = getint(11, 10, "Y? ");

  clear();

  mvprintw(y, x, "Hello, world!");
  mvprintw(LINES-1, 0, "Press any key to exit");
  refresh();

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