String input problems

Programming, for all ages and all languages.
Post Reply
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

String input problems

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: String input problems

Post 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...
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Re: String input problems

Post 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;
}
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: String input problems

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: String input problems

Post by DeletedAccount »

he he .. sometimes , input problems occur with scanf () etc... fflush(stdin) ; will usually fix it :)
Post Reply