problem with my gets function !

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

However, you've been such a pain in the backside in the last few days that I don't feel inclined to look at your problem
Hmm.. somebody got out of bed with the wrong foot. :wink:

but then again people who write *string++; instead of string++; for advancing the pointer only should learn a little more about the language they are writing in.

Nevertheless, try separating the functions:

Code: Select all

char getch() {
    //- code for getting a single character: 
    //- the problem will be in this function.
}

void gets(char * line) {
    char ch;
    while((ch = getch()) != '\n') *line++ = ch;
}
Last edited by os64dev on Fri Sep 28, 2007 1:55 am, edited 1 time in total.
Author of COBOS
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

os64dev wrote:but then again people who write *string++; instead of string++; for advancing the pointer only should learn a little more about the language they are writing in.
It's valid - the postfix '++' occurs after the dereferencing and the data is assigned.

At least, it works for me :D
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

i didn't talk about your code :? *string++ = scancode; is valid and is also how i would use it. i meant the code of mohammed who wrote *string++; just to advance the string see

Code: Select all

gets(unsigned char *string)
{
  int strcounter = 0;
  while (1)
  {
     while (! (inportb(0x64) & 0x1) );
    scancode = kbdus[inportb(0x60)];
    if(scancode != '\n'&&)
    {
       putch(scancode);
      *string=scancode;
     *string++;
   
    }
    else
      *string++;
      *string = '\0';
      return *string;
  }
}
Author of COBOS
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

os64dev wrote:i didn't talk about your code :? *string++ = scancode; is valid and is also how i would use it.
Oops, sorry! Should've read that post a bit more carefully... :oops:
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

What do you think of this :

Code: Select all

char getc()
{
unsigned char c;
while (! (inportb(0x64) & 0x1) ) ;
c = inportb(0x60);
putch(kbdus[c]);
return c;
}
gets(unsigned char *string)
{
char ch;
while(ch=getc() != '\n')
{
*string++;
}
*string++ = '\0';
}
but it didn't want to stop accept key strikes ! when i press enter it started new line!
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

when i press enter it started new line!
Read your code and figure it out for yourself. It's not a difficult problem to figure out.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Dear Jeebus, where did you learn to code? I count two MAJOR flaws with that code:

1) The one that you mentioned. I'm not going to give it to you, work it out yourself.
2) look at this line:

Code: Select all

*string++;
- and try and work out why your string will contain nothing but a null-terminator when your function returns ;)

I really think your C coding skills aren't up to scratch. Maybe you should try programming something simpler before an OS, because I for one will not help you with such trivial problems.
Post Reply