here is the sequence i'm using for my arrow keys

#include<stdio.h>

unsigned char keyin

main()
{

if(square==1)
    {//start square
    putimage(285,440,ste,COPY_PUT);
    while((keyin=getch())!='\r')
      {//up arrow key
      if(keyin==72)
	{//hit up arrow
	keyin=getch();
	square=2;
	}//hit up arrow
      }//up arrow key
    }//start square
  do
    {//game from start square on
    if(square==2)
      {//on square 2
      putimage(285,400,ste,COPY_PUT);
      }//on square 2
    square=17;
    if(square==17)
      {//on the final square, you win
      gamer=1;
      win=1;
      }//on the final square, you win
    }//game from start square on
  while(gamer!=1);
  cout<<"the while statement has closed.\n";  */
  getch();

}

<< moderator edit: added [code][/code] tags >>

the problem i'm having is that if you hit the arrow key, nothing happens.
if you then hit the enter key twice, what i want to happen, will happen.
if you try to just hit the enter key without first hitting the arrow key it will do nothing and just exit from all the if, do, and while statements.

please please help my game is due in like 8 hours!!!

Recommended Answers

All 8 Replies

Hello... your code... is well.... um, yeah... here is a very basic example of how to get input from the keyboard in C (in the console) that I whipped up just for you

Generated: Fri May 13 01:22:44 2005

[[b][u]001[/u][/b]]     [b]#include <curses.h>[/b]
[[b][u]002[/u][/b]]
[[b][u]003[/u][/b]]     int main()
[[b][u]004[/u][/b]]     {
[[b][u]005[/u][/b]]             initscr();
[[b][u]006[/u][/b]]             [i]/* Input characters one by one */[/i]
[[b][u]007[/u][/b]]             cbreak();
[[b][u]008[/u][/b]]             [i]/* Do not show us what we type */[i]
[[b][u]009[/u][/b]]             noecho();
[[b][u]010[/u][/b]]
[[b][u]011[/u][/b]]             [i]/* Keyboard mapping, stdscr is the typic 24x80 console */[i]
[[b][u]012[/u][/b]]             keypad(stdscr, TRUE);
[[b][u]013[/u][/b]]
[[b][u]014[/u][/b]]             printw("subtronic's little curses demo\n");
[[b][u]015[/u][/b]]
[[b][u]016[/u][/b]]             /* Best way to represent an infinite loop :-) */
[[b][u]017[/u][/b]]             for (;;) {
[[b][u]018[/u][/b]]                     switch (getch()) {
[[b][u]019[/u][/b]]                     case KEY_UP:
[[b][u]020[/u][/b]]                             printw("UP\n");
[[b][u]021[/u][/b]]                             break;
[[b][u]022[/u][/b]]                     case KEY_DOWN:
[[b][u]023[/u][/b]]                             printw("DOWN\n");
[[b][u]024[/u][/b]]                             break;
[[b][u]025[/u][/b]]                     case KEY_LEFT:
[[b][u]026[/u][/b]]                             printw("LEFT\n");
[[b][u]027[/u][/b]]                             break;
[[b][u]028[/u][/b]]                     case KEY_RIGHT:
[[b][u]029[/u][/b]]                             printw("RIGHT\n");
[[b][u]030[/u][/b]]                             break;
[[b][u]031[/u][/b]]                     }
[[b][u]032[/u][/b]]             }
[[b][u]033[/u][/b]]
[[b][u]034[/u][/b]]             endwin();
[[b][u]035[/u][/b]]
[[b][u]036[/u][/b]]             return 0;
[[b][u]037[/u][/b]]     }

Compile with -lcurses

not sure what code that is, i'm using turbo c++ 3.0, does that make a difference? and as far as the code goes i just put in exactly what my computer science teacher gave me so.......

not sure what code that is, i'm using turbo c++ 3.0, does that make a difference? and as far as the code goes i just put in exactly what my computer science teacher gave me so.......

Well why didn't you say any of this in your first post? As far as I know, "stdio.h" is part of the standard C library. I had to infer from your ugly ass code what you were trying to do and in what language. There's an old addage you should internalize, "HELP ME HELP YOU".

Well why didn't you say any of this in your first post? As far as I know, "stdio.h" is part of the standard C library. I had to infer from your ugly ass code what you were trying to do and in what language. There's an old addage you should internalize, "HELP ME HELP YOU".

Oh and I fixed my little C program to correctly add in the bbCode for preprocessor directives and comments :-)... I really detest DaniWeb's "You have thirty minutes to edit your post" rule :/

[b]Exemplify v0.1 (by subtronic[/b])
[i]Generated: Fri May 13 09:40:57 2005[/i]
 
 
[[b][u]001[/u][/b]]     [b]#include <curses.h>[/b]
[[b][u]002[/u][/b]]
[[b][u]003[/u][/b]]     [i]// Let us test all comments :-)[/i]
[[b][u]004[/u][/b]]
[[b][u]005[/u][/b]]     int main()
[[b][u]006[/u][/b]]     {
[[b][u]007[/u][/b]]             initscr();
[[b][u]008[/u][/b]]             [i]/* Input characters one by one */[/i]
[[b][u]009[/u][/b]]             cbreak();
[[b][u]010[/u][/b]]             [i]/* Do not show us what we type */[/i]
[[b][u]011[/u][/b]]             noecho();
[[b][u]012[/u][/b]]
[[b][u]013[/u][/b]]             [i]/* Keyboard mapping, stdscr is 24x80 console */[/i]
[[b][u]014[/u][/b]]             keypad(stdscr, TRUE);
[[b][u]015[/u][/b]]
[[b][u]016[/u][/b]]             printw("subtronic's little curses demo\n");
[[b][u]017[/u][/b]]
[[b][u]018[/u][/b]]             [i]/* Best way to represent an infinite loop :-) */[/i]
[[b][u]019[/u][/b]]             for (;;) {
[[b][u]020[/u][/b]]                     switch (getch()) {
[[b][u]021[/u][/b]]                     case KEY_UP:
[[b][u]022[/u][/b]]                             printw("UP\n");
[[b][u]023[/u][/b]]                             break;
[[b][u]024[/u][/b]]                     case KEY_DOWN:
[[b][u]025[/u][/b]]                             printw("DOWN\n");
[[b][u]026[/u][/b]]                             break;
[[b][u]027[/u][/b]]                     case KEY_LEFT:
[[b][u]028[/u][/b]]                             printw("LEFT\n");
[[b][u]029[/u][/b]]                             break;
[[b][u]030[/u][/b]]                     case KEY_RIGHT:
[[b][u]031[/u][/b]]                             printw("RIGHT\n");
[[b][u]032[/u][/b]]                             break;
[[b][u]033[/u][/b]]                     }
[[b][u]034[/u][/b]]             }
[[b][u]035[/u][/b]]
[[b][u]036[/u][/b]]             endwin();
[[b][u]037[/u][/b]]
[[b][u]038[/u][/b]]             return 0;
[[b][u]039[/u][/b]]     }

Im using gcc and mingw and im not able to use curses ... is it non included library? I did try -lcurses

If I recall correctly, mingw doesn't support curses "out of the box". You can download and install PDcurses from http://pdcurses.sourceforge.net/. Or if you still want to use getch outside of the curses library, this might work for you:

#include <conio.h> /* Or whatever header getch may be defined in */
#include <ctype.h>
#include <stdio.h>

enum {
  UP = 72,
  LT = 75,
  RT = 77,
  DN = 80,
};

int keycode(int c)
{
  return (c == 0 || c == 224) ? getch() : c;
}

int main(void)
{
  int c;

  while ((c = getch()) != '\r') {
    switch (keycode(c)) {
      case UP: puts("Up"); break;
      case DN: puts("Down"); break;
      case LT: puts("Left"); break;
      case RT: puts("Right"); break;
      default: 
        if (isprint(c))
          printf("%c\n", c);
        else
          printf("%d\n", c);
    }
  }

  return 0;
}

If your compiler doesn't support getch, you have to write it yourself. Here's a Unix version using termios:

#include <stdio.h> 
#include <termios.h> 
#include <unistd.h> 

int getch(void) 
{
  int c;
  struct termios old, new;
  
  tcgetattr(STDIN_FILENO, &old);
  new = old;
  new.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &new);
  c = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &old);
  
  return c;
}

Worked great thanks soo much :)

Anytime. Glad I could help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.