Reputation: 3
#include <ncurses.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[]){
int x,y,ch;
if(argc != 2){
fprintf(stderr,"there is no value to be show\n");
exit(1);
}
getmaxyx(stdscr,y,x);
initscr();
start_color();
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_WHITE,COLOR_WHITE, COLOR_BLACK);
keypad(stdscr,TRUE);
crmode();
noecho();
while(ch != 'q'){
mvprintw(y/2, x/2, "%s", argv[1]);
refresh();
ch = getch();
if(ch == 'u'){
attron(COLOR_PAIR(COLOR_GREEN));
mvprintw(y/2, x/2, "%s", strupr(argv[1]));
}
else if(ch == 'l'){
attron(COLOR_PAIR(COLOR_RED));
mvprintw(y/2, x/2, "%s", argv[1]);
}
else if(ch == 'o'){
attron(COLOR_PAIR(COLOR_WHITE));
mvprintw(y/2, x/2, "%s", argv[1]);
}
else{
continue;
}
}
endwin();
return 0;
}
So from this code I want to display string argv[1]
at the center of the terminal. When the user press u
I want to make it uppercase when the user press l
I want to make it into the lowercase. At my code when I want to make the word into the uppercase I want to use strupr
to make it into uppercase but cannot compile this code properly.
It said implicit declaration for the strupr
function.
And I think for putting into the center of the terminal I already make the proper program by using mvprintw(y/2,x/2)
but despite of the word displayed at the center it displayed at the top left of the terminal.
Is there somebody know where my mistake at?
Upvotes: 0
Views: 579
Reputation: 241931
You can't query getmaxyx
until after you have initialized the screen with initscr
. Or, better said, you can do the query but you won't get meaningful information.
strupr
is not a standard C function, and I guess your C library doesn't implement it. You can implement it by looping over the string calling toupper
:
char* my_strupr(char* str) {
for (unsigned char* p = (unsigned char*)str; *p; ++p) {
*p = toupper(*p);
}
return str;
}
That's not the best way of converting to upper case. It won't work if you have any non-ascii characters in your input. There are libraries which provide Unicode-aware string transformations, and you should think about using one of them. (Also, you might want to use the wide-char-enable ncurses
. See the manual for details.)
Upvotes: 1