Ncurses form can't be read out correctly

This is a discussion on Ncurses form can't be read out correctly within the C Programming forums, part of the General Programming Boards category; Hi First of all... I hope this is the right forum for the question... *cough* I started with C only ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    5

    Ncurses form can't be read out correctly

    Hi

    First of all... I hope this is the right forum for the question... *cough*

    I started with C only a few days ago (already programmed in Python before) and I currently have a really strange problem.

    I built a small ncurses interface with a form that has one field. (this field is as wide as the terminal). Now the user can type into this form and when he presses Enter the text in the form is supposed to be printed in a different window.
    Actually doesn't sound that hard... but it does some really strange things.

    1. if the text that was inserted is not longer than the form itself is, the text output is just whitespaces (exactly as long as the form is)
    2. if the text is longer than the form itself (so it seems to be cleared and it starts again at the beginning) the output is this "first line" (so the output is again as long as the form... but this time it contains the text inserted...)

    I hope this is at least partially understandable...

    heres the code snippet where input is checked and manipulated

    Code:
    while((charinput=getch()) != KEY_END)
    	{
    		switch(charinput)
    		{
    			case 10:
    				snprintf(inputstring, 200, "%s", field_buffer(field[0], 0));
    				wprintw(chatwin, "%s|\n", field_buffer(field[0], 0));
    				wrefresh(chatwin);
    				set_field_buffer(field[0], 0, "");
    				wrefresh(entrywin);
    				break;
    			default:
    				form_driver(fo, charinput);
    				wrefresh(inputwin);
    		}
    	}
    I'm sure there is some stupid mistake I did...
    Is there maybe a problem because of the Enter-key?

    I hope somebody can help me..

    greetings
    vIiRuS

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Not enough code posted to be able to help.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    5
    oh... okay

    Code:
    #include <form.h>
    #include <curses.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    WINDOW *chatwin, *entrywin;
    FIELD  *field[1];
    FORM   *fo;
    
    void quit(void)
    {
    	int i;
    	unpost_form(fo);
    	free_form(fo);
    
    	free_field(field[0]);
    	free_field(field[1]);
    
    	delwin(chatwin);
    	endwin();
    }
    
    int main(void)
    {
    	int xsize, ysize;
    	int charinput, i;
    	char inputstring[200];
    	char ttime[10];
    	initscr();
    	atexit(quit);
    	clear();
    	noecho();
    	curs_set(1);
    	cbreak();
    	keypad(stdscr, TRUE);
    
    	getmaxyx(stdscr, ysize, xsize);
    
    	start_color();
    	use_default_colors();
    	init_pair(1, COLOR_YELLOW, COLOR_BLUE);
    	init_pair(2, COLOR_BLUE, COLOR_WHITE);
    
    	chatwin = newwin((ysize - 8), (xsize-21), 6, 21);
    
    	entrywin = newwin(1, (xsize-21), (ysize - 1), 21);
    	
    	field[0] = new_field(1, (xsize - 21), 0, 0, 0, 10);
    	field[1] = 0;
    	set_form_win(fo, entrywin);
    	fo = new_form(field);
    	post_form(fo); 
    	field_opts_on(field[0], O_STATIC);
    	set_field_fore(field[0], COLOR_PAIR(2));
    	set_field_back(field[0], COLOR_PAIR(2));
    
    	refresh();
    	wrefresh(chatwin);
    	wrefresh(entrywin);
    
    	while((charinput=getch()) != KEY_END)
    	{
    		switch(charinput)
    		{
    			case 10:
    				snprintf(inputstring, 200, "%s", field_buffer(field[0], 0));
    				struct tm *akttime;
    				time_t second;
    				time(&second);
    				akttime = localtime(&second);
    				strftime(ttime, 10, "%H:%M:%S", akttime);
    				wprintw(chatwin, "<%s> %s|\n", ttime, inputstring);
    				wrefresh(chatwin);
    				set_field_buffer(field[0], 0, "");
    				wrefresh(entrywin);
    				break;
    			default:
    				form_driver(fo, charinput);
    				wrefresh(entrywin);
    		}
    	} 
    
    	return(0);
    }
    the code can be compiled and should "work"...

    greetings
    vIiRuS

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    5
    *push*

    doesn't anybody have an idea what I did wrong? :/

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It seems odd that you would define a FIELD instance, and then not use it. Instead, you are passing the char from field(0) around.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    5
    Quote Originally Posted by Dino View Post
    It seems odd that you would define a FIELD instance, and then not use it. Instead, you are passing the char from field(0) around.
    I'm sorry but I don't quite understand what you mean...

    do you mean the "field[1] = 0;" line? the example file I have also has an extra one that isn't used... isn't that correct?

    and what do you mean that I am passing the char from field(0) around?

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    FIELD  *field[1];
    
    field[1] = 0;   // this is overwriting memory. valid index is 0 ONLY!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you are not going to post all your code, at least post your function and variable declarations.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    5
    Quote Originally Posted by Dino View Post
    If you are not going to post all your code, at least post your function and variable declarations.
    that actually _was_ the whole code...

    I rechecked the code example and they had it a bit different... but that actually wasn't the problem

    The problem is that you can't read out the buffer of a field if the cursor is pointing on it. So I have to move the cursor to the empty field before I read out the buffer and then move the cursor back to the field afterwards.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Glad you figured it out.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused with read()
    By _EAX in forum Linux Programming
    Replies: 2
    Last Post: 03-17-2008, 04:14 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 05:45 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21