www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - getch() function

reply "Bruno A. Costa" <bruno codata.com.br> writes:
Hi,

I'm trying to compile some programs that use the getch() function,
but they fail to link:

[bruno localhost D]$ dmd char.d
gcc char.o -o char -lphobos -lpthread -lm
char.o(.gnu.linkonce.t_Dmain+0x30): In function `_Dmain':
: undefined reference to `getch'
collect2: ld returned 1 exit status
--- errorlevel 256

Example from dsource.org:

import std.c.stdio;
import std.string;

void main()
{ 
    char k;

    printf("\nI'm going to ask you to press 10 keys (I promise it's
painless).\n"
    "Certain keys don't count, but the whole alphabet is fair game.\n\n");

    for(int i=0; i<10; i++)
    {
        printf("Press key: %d\t", i);
        k = getch();
        printf("[%c]\t", k);            /* print the character pressed */
        printf("[%d]\n", cast(int) k);  /* print ASCII code for key */
    }
    printf("\nThe End.\n\n");
}

I'm using dmd-0.90 in a Fedora Linux Core 2, gcc version 3.3.3

Hints???


Thanks,
Bruno.
May 28 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"Bruno A. Costa" <bruno codata.com.br> wrote in message
news:c97bq0$18nm$1 digitaldaemon.com...
 Hi,

 I'm trying to compile some programs that use the getch() function,
 but they fail to link:

 [bruno localhost D]$ dmd char.d
 gcc char.o -o char -lphobos -lpthread -lm
 char.o(.gnu.linkonce.t_Dmain+0x30): In function `_Dmain':
 : undefined reference to `getch'
 collect2: ld returned 1 exit status
 --- errorlevel 256

 Example from dsource.org:

 import std.c.stdio;
 import std.string;

 void main()
 {
     char k;

     printf("\nI'm going to ask you to press 10 keys (I promise it's
 painless).\n"
     "Certain keys don't count, but the whole alphabet is fair game.\n\n");

     for(int i=0; i<10; i++)
     {
         printf("Press key: %d\t", i);
         k = getch();
         printf("[%c]\t", k);            /* print the character pressed */
         printf("[%d]\n", cast(int) k);  /* print ASCII code for key */
     }
     printf("\nThe End.\n\n");
 }

 I'm using dmd-0.90 in a Fedora Linux Core 2, gcc version 3.3.3

 Hints???


 Thanks,
 Bruno.

It's a Windows function (or Linux if you're using some term library). Use getchar(). getch() returns immediatly when a key is pressed, but getchar() waits until return is pressed and is portable.
May 28 2004
parent "Bruno A. Costa" <bruno codata.com.br> writes:
Vathix wrote:

 "Bruno A. Costa" <bruno codata.com.br> wrote in message
 news:c97bq0$18nm$1 digitaldaemon.com...
 Hi,

 I'm trying to compile some programs that use the getch() function,
 but they fail to link:

 [bruno localhost D]$ dmd char.d
 gcc char.o -o char -lphobos -lpthread -lm
 char.o(.gnu.linkonce.t_Dmain+0x30): In function `_Dmain':
 : undefined reference to `getch'
 collect2: ld returned 1 exit status
 --- errorlevel 256

 Example from dsource.org:

 import std.c.stdio;
 import std.string;

 void main()
 {
     char k;

     printf("\nI'm going to ask you to press 10 keys (I promise it's
 painless).\n"
     "Certain keys don't count, but the whole alphabet is fair
     game.\n\n");

     for(int i=0; i<10; i++)
     {
         printf("Press key: %d\t", i);
         k = getch();
         printf("[%c]\t", k);            /* print the character pressed */
         printf("[%d]\n", cast(int) k);  /* print ASCII code for key */
     }
     printf("\nThe End.\n\n");
 }

 I'm using dmd-0.90 in a Fedora Linux Core 2, gcc version 3.3.3

 Hints???


 Thanks,
 Bruno.

It's a Windows function (or Linux if you're using some term library). Use getchar(). getch() returns immediatly when a key is pressed, but getchar() waits until return is pressed and is portable.

Yes it is a windows function. But I thought that using a standard library (phobos) I would get portable code. Shouldn't phobos be more portable? Thanks, Bruno.
May 28 2004