Please Review my Program

This is a discussion on Please Review my Program within the C Programming forums, part of the General Programming Boards category; I don't really have any specific problem I'm trying to fix. This is my first "graphics" program, which moves an ...

  1. #1
    Unregistered
    Guest

    Please Review my Program

    I don't really have any specific problem I'm trying to fix. This is my first "graphics" program, which moves an asterisk around the screen using the arrow keys. If one of you experienced programmers could look over this and tell me if there was a better method of going about this task, please tell me. I have noticed the bug of being able to move off the screen, but haven't tried to fix it yet. I've only been programming for maybe a month ( I'm learning from a book). Somebody please tell me how I'm doing. By the way, I'm running under windows.

    #define ENTER '\r'
    #define UP 72
    #define DOWN 80
    #define LEFT 75
    #define RIGHT 77

    main()
    {
    int key,xval,yval;

    xval=yval=0;

    clrscr();
    printf("Use the arrow keys to move the dot, ENTER to quit: ");
    gotoxy(40,12);
    printf("*");

    getkey:
    key=getch();
    switch(key)
    {
    case 0:
    key=getch();
    break;
    case ENTER:
    exit(EXIT_SUCCESS);
    default:
    goto getkey;
    }
    gotoxy(xval+40,yval+12);
    printf(" ");
    switch(key)
    {
    case UP:
    --yval;
    gotoxy(xval+40,yval+12);
    printf("*");
    goto getkey;
    case DOWN:
    ++yval;
    gotoxy(xval+40,yval+12);
    printf("*");
    goto getkey;
    case LEFT:
    --xval;
    gotoxy(xval+40,yval+12);
    printf("*");
    goto getkey;
    case RIGHT:
    ++xval;
    gotoxy(xval+40,yval+12);
    printf("*");
    goto getkey;
    }
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If one of you experienced programmers could look over this and
    >tell me if there was a better method of going about this task
    I had to define my own clrscr and gotoxy because Borland is crazy about nonstandard functions, but this seems to work peachy except for the off the screen bug:
    Code:
    void print ( int x, int y )
    {
      clrscr();
      gotoxy( x, y ); 
      printf ( "*" );
    }
    
    int main ( void ) 
    { 
      int key = 0, xval = 40, yval = 12;
    
      printf("Use the arrow keys to move the dot, ENTER to quit: "); 
      print ( xval, yval ); 
    
      while ( key != ENTER ) {     
        switch ( ( key = getch() ) ) 
        { 
        case UP:    print ( xval, --yval ); break;  
        case DOWN:  print ( xval, ++yval ); break;
        case LEFT:  print ( --xval, yval ); break;
        case RIGHT: print ( ++xval, yval ); break;
        }
      }
      return EXIT_SUCCESS;
    }
    The bug I'll leave to you since it's simple enough to fix with a little extra testing. Upon running your program I noticed that the asterisk tended towards multiplying instead of remaining the only one and while I could move it down and to the right, up and left had problems.

    In the case of this program, a loop is drastically easier to follow than goto and one of your switch statements was apparently redundant. If you had a reason for the first one let me know because I removed it. You'll notice that I moved the relocation and printing operation to a single function since you use it quite a bit, this makes the code shorter and a bit more elegant than the way you had it.

    Notes:
    Please use the code tags from now on, it makes helping you quite a bit easier if we don't have to reformat the code first. And when you paste code, be sure to include the includes as that makes it easier to determine both what platform and compiler you have, but also saves us having to guess or rewrite your code to cover for an unknown feature.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    Talking

    thank you for your help. my includes were only stdio, stdlib, and math, but I don't know if I really needed stdlib. I just needed that first switch statement because that's how the book said to get the arrow keys. I work under Win98 and DJGPP(gcc). THANK YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  4. review my telephone network simulation program
    By debuger2004 in forum C Programming
    Replies: 3
    Last Post: 06-20-2003, 02:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21