Unix Programming - ncurses troubles

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > ncurses troubles





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ncurses troubles
mr_semantics@hotmail.com

2005-08-21, 7:48 am

I have been fiddling with ncurses, particularly with the material found
in
the book 'Beginning Linux Programming, 3rd edition' - anyway, there is
a
program on page 216 that I can't get to work properly. Here is the
code
that I believe is causing the trouble:


move(9, 10);
printw("%s", "Password: ");
refresh();

cbreak();
noecho();

memset(password, '\0', sizeof(password));
while (i < PW_LEN) {
password[i] = getch();
move(9, 20 + i);
addch('*');
refresh();
if (password[i] == '\n') {
break;
}
if (strcmp(password, real_password) == 0) {
break;
}
i++;
}

echo();
nocbreak();

move(11, 10);
if (strcmp(password, real_password) == 0) {
printf("%s", "Correct");
} else {
printf("%s", "Wrong");
}

refresh();
endwin();

return 0;
}

....in particular, the program executes (seemingly) properly until the
statements:
if (strcmp(password, real_password) == 0) {
printf("%s", "Correct");
} else {
printf("%s", "Wrong");
}

It does not print out anything at all. I enter the password, and then
the
program terminates. I have stepped through the program with a
debugger,
and when I enter the correct password, it indeed says 'Correct', but
only
in the debugger. Any help would be appreciated.

Rob Somers

Thomas Dickey

2005-08-21, 7:48 am

mr_semantics@hotmail.com wrote:

> ...in particular, the program executes (seemingly) properly until the
> statements:
> if (strcmp(password, real_password) == 0) {
> printf("%s", "Correct");
> } else {
> printf("%s", "Wrong");
> }


> It does not print out anything at all. I enter the password, and then the
> program terminates. I have stepped through the program with a debugger, and
> when I enter the correct password, it indeed says 'Correct', but only in the
> debugger. Any help would be appreciated.


Normally you'd use printw() rather than printf() to write to the screen.
While it is possible to mix the two, you have to keep in mind that (n)curses
doesn't know what was done with the printf. The refresh() does nothing,
since there were no changes to stdscr since the last refresh() call.

Adding a fflush(stdout) after the printf's will get the text displayed,
but using printw is preferable, since then (n)curses will know where the
cursor is.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com