Page 1 of 1

String input problems

Posted: Wed Aug 20, 2008 6:15 pm
by piranha
I have an annoying problem, that I probably know the answer to, but it might be my sleep-lesss angry tired nees that makes it so that I can't figure it out.

Here is the code:

Code: Select all

#include <stdio.h>
int main()
{
	char buf[256];
	char str[1];
	int i;
	char c;
	buf[0] = '\0';
	i = 0;
	while (1) {
		c = getchar();putchar('Q');
		if (c == '\r' || c == '\n') {
			break;
		}
		if (c == '\b' || c == 0x7f) {
			if (i > 0) {
				i--;
				buf[i] = '\0';
			}
			continue;
		}
		buf[i] = c;
		str[0] = buf[i];
		str[1]=0;
		i++;
	}
	buf[i] = '\0';
	printf("%d\n%s\n", i, buf);
	return 0;
}
This is a simple thing to fill a buffer with keyboard input. It should print the character 'Q' as you typed (you type 'c', and 'cQ' should be displayed)

However, when you type 'hello', only that is displayed, as if the putchar() isn't even in there! When you press enter, then it prints out 'QQQQQ5'. And the string isn't printed, though it should be.

This has been annoying me for a while, can someone help?

-JL

Re: String input problems

Posted: Wed Aug 20, 2008 6:54 pm
by Alboin
getch expects a newline.

You could use curses...

Code: Select all

#include <stdio.h>
#include <curses.h>

int main()
{
   char buf[256];
   char str[1];
   int i;
   char c;
   buf[0] = '\0';
   i = 0;
   
   initscr();
   keypad(stdscr, TRUE);
   nonl();
   cbreak();
   
   while (1) {
      c = getchar();addch(c);addch('Q');
      if (c == '\r' || c == '\n') {
         break;
      }
      if (c == '\b' || c == 0x7f) {
         if (i > 0) {
            i--;
            buf[i] = '\0';
         }
         continue;
      }
      buf[i] = c;
      str[0] = buf[i];
      str[1]=0;
      i++;
      
      refresh();
   }
   buf[i] = '\0';
   printw("%d\n%s\n", i, buf);
   
   endwin();
   return 0;
}
It seems to produce the output you require. I haven't tested the buffer, however...

Re: String input problems

Posted: Wed Aug 20, 2008 6:59 pm
by xyjamepa
humm...try this:

Code: Select all

#include <stdio.h>
int main()
{
   char buf[256];
   char str[1];
   int i,j;
   char c;
   buf[0] = '\0';
   i = 0;
   while (1) 
   {
      
      c = getch();

      if(c!=0x0D)		//if it is Enter don't print 'Q'
       putchar('Q');

      putchar(c);
      if (c == '\r' || c == '\n') 
      {
         break;
      }
      else if (c == '\b' || c == 0x7f) 
       {
         if (i > 0) 
         {
            i--;
            buf[i] = '\0';
         }
         continue;
       }
      buf[i] = c;
      str[0] = buf[i];
      //str[1]=0;
      i++;
   }
   buf[i++] = '\0';
   putchar('\n');
   printf("%d\n%s\n", --i, buf);
   return 0;
}

Re: String input problems

Posted: Wed Aug 20, 2008 9:23 pm
by piranha
Well, I can't use ncurses, because I'm using svgalib for the actual program...
Thanks abuashraf, that worked...Damn, I can't believe I overlooked that.. :oops:

-JL

Re: String input problems

Posted: Thu Aug 21, 2008 3:37 am
by DeletedAccount
he he .. sometimes , input problems occur with scanf () etc... fflush(stdin) ; will usually fix it :)