Thread overview
ways to output a char?
Dec 28, 2004
Asaf Karagila
Dec 28, 2004
Asaf Karagila
Dec 28, 2004
ibisbasenji
Dec 29, 2004
Asaf Karagila
Dec 29, 2004
Asaf Karagila
Dec 29, 2004
Derek
Dec 29, 2004
kris
December 28, 2004
Hi,
i'm trying to output a char to the screen,
the char is inputted by the user.

when i write normally, it's ok. but when i hit enter or something like that, it shows ascii symbols and not really going down like '\n' would produce.

any idea how i can change it ?

read code

# r[1] = cast (int) getch();

write code

# printf("%c",cast(char)r[1]);

i used printf because it's 1 char, and i couldn't get writef to work properly..

- Asaf.


December 28, 2004
I don't know why you're casting to an int, you're probably doing something that you didn't post.  But some consoles will not display anything unless they hit a newline or you manually flush them.  I hope this is what you wanted:

void main()
{
 char c=0;
 while(c!='q')
 {
  c=getch();
  writef(c);
  fflush(stdout);
 }
}


December 28, 2004
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:cqsd2l$1t6v$1@digitaldaemon.com...
>I don't know why you're casting to an int, you're probably doing something
> that you didn't post.  But some consoles will not display anything unless they hit a newline or you manually flush them.  I hope this is what you wanted:
>
> void main()
> {
> char c=0;
> while(c!='q')
> {
>  c=getch();
>  writef(c);
>  fflush(stdout);
> }
> }
>
>

this is the error i get when i do it:

c:\d\dmd\bin\..\src\phobos\std\stream.d(2477): variable std.stream.stdout
confli
cts with std.c.stdio.stdout at c:\d\dmd\bin\..\src\phobos\std\c\stdio.d(134)
emu.d(130): function std.c.stdio.fflush (_iobuf *) does not match argument
types
 (File )
emu.d(130): cannot implicitly convert expression stdout of type File to
_iobuf *


and when i changed the code to

# writef(cast(char)r[1]);

(yes, this is an int type array. but i only cares about the lower 8bit..) the screen stayed blank. nothing is coming out of it..

- Asaf.


December 28, 2004
import std.c.stdio;
import std.stdio;

alias std.c.stdio.stdout cout;

void main()
{
char c=0;
while(c!='q')
{
c=getch();
writef(c);
fflush(cout);
}
}

-- Chris Sauls
December 29, 2004
ok, it compiles, but it's still not working.
any other ideas ?
maybe if i'll have a switch that checks if it's a special char
(enter/backspace/etc)
and if it is, it will print the code like,
switch(c){
case 13:
        writef("\n");
        break;
case x:
        writef(x's code);
        break;
default:
        writef(c);
};
what codes do i need to take in mind ?

- Asaf.

<ibisbasenji@gmail.com> wrote in message news:cqslff$25pp$1@digitaldaemon.com...
> import std.c.stdio;
> import std.stdio;
>
> alias std.c.stdio.stdout cout;
>
> void main()
> {
> char c=0;
> while(c!='q')
> {
> c=getch();
> writef(c);
> fflush(cout);
> }
> }
>
> -- Chris Sauls


December 29, 2004
It sounds to me that you just want to rewrite gets().

import std.stdio, std.string;

void main()
{
 char[] s;
 s.length=80;
 s=toString(gets(s));
 writefln(s);
}

I take it there is a reason you want to make your own input function?  I mean, besides using an outdated C function..


December 29, 2004
On Tue, 28 Dec 2004 21:24:14 +0200, Asaf Karagila wrote:

> Hi,
> i'm trying to output a char to the screen,
> the char is inputted by the user.
> 
> when i write normally, it's ok. but when i hit enter or something like that, it shows ascii symbols and not really going down like '\n' would produce.
> 
> any idea how i can change it ?
> 
> read code
> 
> # r[1] = cast (int) getch();
> 
> write code
> 
> # printf("%c",cast(char)r[1]);
> 
> i used printf because it's 1 char, and i couldn't get writef to work properly..
> 
> - Asaf.

For what its worth, here is a routine that I use ...

<code>
import std.stdio;
private static {
    char kCR = 13;
    char kLF = 10;
}
/* -------------------  get_console_string  ------------------------
   This gets a ASCII string from the console and returns it to the caller.

   Parameters:
     1  ..  in bool pEcho = true;
     2  ..  in uint pMax = 0;

     return .. char[] OutString;

  pEcho determines whether or not the user's keystrokes are echoed
        to the screen. If 'true' then they are seen as they are typed in,
        otherwise they are hidden from view while being typed in.

  pMax is the maximum number of characters that can be returned in the
       output string. If the user types in more than pMax, then the
       excess characters are just ignored.
       If pMax is zero, then there is no maximum and the user can type in
       as many as RAM allows.

  OutString is the character array containing the characters accepted
       by the routine. This string is not returned until the user presses
       the return key.

 Example:
    char[] test;
    // Get a 16-char field with console display
    test = get_console_string(true, 16);
    // Get a field without console display
    test = get_console_string(false);
*/
char[] get_console_string(in bool pEcho = true, in uint pMax = 0)
{
    char    lInChar;
    char[]  lOutString;

    while (true)
    {
        lInChar = cast(char)getch();
        version(Windows)
        {
            if (lInChar == kCR)
            {
                if (pEcho){
                    fputc(lInChar, stdout);
                    fputc(kLF, stdout);
                    fflush(stdout);
                }
                break;
            };

        };

        version(linux)
        {
            if (lInChar == kLF)
            {
                if (pEcho){
                    fputc(lInChar, stdout);
                    fflush(stdout);
                }
                break;
            };

        };

        if  ((pMax == 0) || (pMax > lOutString.length))
        {
            if (pEcho)
            {
                fputc(lInChar, stdout);
                fflush(stdout);
            };

            // Append the char to the output lOutString.
            lOutString ~= lInChar;
        };

    };
    return lOutString;
}

debug{
void main()
{
    writefln("1 '%s'", get_console_string());
    writefln("2 '%s'", get_console_string(false));
    writefln("3 '%s'", get_console_string(true, 16));
    writefln("4 '%s'", get_console_string(false, 8));
}
}
</code>
-- 
Derek
Melbourne, Australia
December 29, 2004
Derek wrote:
> On Tue, 28 Dec 2004 21:24:14 +0200, Asaf Karagila wrote:
> 
> 
>>Hi,
>>i'm trying to output a char to the screen,
>>the char is inputted by the user.
>>
>>when i write normally, it's ok. but when i hit enter or something like that,
>>it shows ascii symbols and not really going down like '\n' would produce.
>>
>>any idea how i can change it ?
>>
>>read code
>>
>># r[1] = cast (int) getch();
>>
>>write code
>>
>># printf("%c",cast(char)r[1]);
>>
>>i used printf because it's 1 char, and i couldn't get writef to work properly..
>>
>>- Asaf.
> 
> 
> For what its worth, here is a routine that I use ...
> 
> <code>
> import std.stdio;
> private static {
>     char kCR = 13;
>     char kLF = 10;
> }
> /* -------------------  get_console_string  ------------------------
>    This gets a ASCII string from the console and returns it to the caller.
> 
>    Parameters:
>      1  ..  in bool pEcho = true;       2  ..  in uint pMax = 0;
>           return .. char[] OutString;
>        pEcho determines whether or not the user's keystrokes are echoed
>         to the screen. If 'true' then they are seen as they are typed in,
>         otherwise they are hidden from view while being typed in.
>           pMax is the maximum number of characters that can be returned in the
>        output string. If the user types in more than pMax, then the
>        excess characters are just ignored.
>        If pMax is zero, then there is no maximum and the user can type in
>        as many as RAM allows.          OutString is the character array containing the characters accepted
>        by the routine. This string is not returned until the user presses
>        the return key.         Example:
>     char[] test;
>     // Get a 16-char field with console display
>     test = get_console_string(true, 16);
>     // Get a field without console display
>     test = get_console_string(false);
> */
> char[] get_console_string(in bool pEcho = true, in uint pMax = 0)
> {
>     char    lInChar;
>     char[]  lOutString;
>         while (true)
>     {
>         lInChar = cast(char)getch();
>         version(Windows)         {
>             if (lInChar == kCR)
>             {
>                 if (pEcho){
>                     fputc(lInChar, stdout);
>                     fputc(kLF, stdout);
>                     fflush(stdout);
>                 }
>                 break;
>             };
>                 };
>                 version(linux)         {
>             if (lInChar == kLF)
>             {
>                 if (pEcho){
>                     fputc(lInChar, stdout);
>                     fflush(stdout);
>                 }
>                 break;
>             };
>                 };
>                 if  ((pMax == 0) || (pMax > lOutString.length))
>         {
>             if (pEcho)
>             {
>                 fputc(lInChar, stdout);
>                 fflush(stdout);
>             };
>                         // Append the char to the output lOutString.
>             lOutString ~= lInChar;
>         };
>             };
>     return lOutString;   }
> 
> debug{
> void main()
> {
>     writefln("1 '%s'", get_console_string());
>     writefln("2 '%s'", get_console_string(false));
>     writefln("3 '%s'", get_console_string(true, 16));
>     writefln("4 '%s'", get_console_string(false, 8));
> }
> }
> </code>

you might also take a look at Mango.io at (dsource.org). It uses a simple & truly typesafe IO model, and tokenizes input when using Stdin. For example:

# private import mango.io.Stdio;

# char c;
# double d;
# int  x;
# char[] s;

# Stdout << "type a string, an integer, a decimal number, and a char: ";
# Stdin >> s >> x >> d >> c;
# Stdout "you typed: " << s << ' ' << x << ' ' << d << ' ' << c;

You could also use the Tokenizer classes directly, and perhaps get a bit fancy by using RegexTokenizer to extract patterns from the input.

If you don't like the C++ stream syntax, use this instead:

# Stdin.get(s).get(x).get(d).get(c);
# Stdout.put(s).put(x).put(d).put(c);
December 29, 2004
well, basicly,
i made up some very idiotic architecture,
i wrote the basic emulation, then i wrote an assembler code,
now to test it i wrote different codes and saw the output,
found bugs here and there, then when i got to the read/wrte codes,
it worked, but it didn't skip the line, and when i backspaced,
it didn't delete the char from the screen. etc etc etc.
i have no clue what's wrong with that. but i came to figure out
that it's something with the special characters.
so, any ideas to solve this would be much appreciated.

- Asaf.
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message
news:cqtcsa$2r8q$1@digitaldaemon.com...
> It sounds to me that you just want to rewrite gets().
>
> import std.stdio, std.string;
>
> void main()
> {
> char[] s;
> s.length=80;
> s=toString(gets(s));
> writefln(s);
> }
>
> I take it there is a reason you want to make your own input function?  I mean, besides using an outdated C function..
>
>