### Draw a line

Hello everyone, I have been searching how to draw a line (horizontal and vertical) using ncurses, but couldn't find any examples. Therefore, I am in a situation I would like to ask for a help. I need to draw two lines: one above Numerical systems and the other beneath 10, 8, 16 and 2. I am struggling to understand how to draw lines using ncurses in mac. I know its a basic question, but cannot understand it:(

Here's my code (whline two commands of 0 and 3 are not working):
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191`` ``````#include #include #include #include #include #include #include #include using namespace std; int main() { initscr(); int i, n, number, number2, number3, number4, value, nlines, ncols, y0, x0, y, x; //WINDOW * win = newwin(nlines,ncols, y0, x0); int whline(WINDOW * win, chtype ch, int m); //getmaxyx(WINDOW *, y, x); string result; whline(win, 0, 3); cout << " " << "** Numerical systems **" << endl << endl; cout << "\"10\"" << "\t" << "\"8\"" << "\t" << "\"16\"" << "\t" << "\"2\"" << endl; whline(win, 0, 3); endwin(); for(i=0, n=0; i<=15 && n<=15; i++, n++) { ostringstream convert; switch(n) { case 0: value=0/2; break; case 1: value=1%2; break; case 2: number=2/2; number2=1/2; convert << number; convert << number2; result = convert.str(); value = stoi(result); break; case 3: number=3%2; number2=1%2; convert << number; convert << number2; result = convert.str(); value = stoi(result); break; case 4: number=4%2; number2=2%2; number3=1%2; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 5: number=5%2; number2=2%2; number3=1%2; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 6: number=6%2; number2=3%2; number3=1%2; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 7: number=7%2; number2=3%2; number3=1%2; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 8: number=8%2; number2=4%2; number3=2%2; number4=1%2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 9: number=9%2; number2=4%2; number3=2%2; number4=1%2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 10: number=10%2; number2=5%2; number3=2%2; number4=1%2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 11: number=11%2; number2=5%2; number3=2%2; number4=1%2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 12: number=12%2; number2=6%2; number3=3%2; number4=2/2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 13: number=13%2; number2=6%2; number3=3%2; number4=2/2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 14: number=14%2; number2=7%2; number3=3%2; number4=2/2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; case 15: number=15%2; number2=7%2; number3=3%2; number4=2/2; convert << number4; convert << number3; convert << number2; convert << number; result = convert.str(); value = stoi(result); break; } printf(" %d \t %o \t %X \t %d \n", i, i, i, value); } }``````
Last edited on
Doesn't anyone know of ncurses? Then maybe someone can suggest me a forum where people using c++ in mac are gathering?
You are missing the chtype (character type), so if you wanted to draw a single line like __________
you need

int whline(WINDOW *win, chtype ch, int n);

so where you have

 `` `` ``whline(win, 0, 3);``

you need

 `` `` ``whline(win, ACS_HLINE, 0, 3);``

There are a few other things wrong with this code including a failure to refresh(), no declaration of the WINDOW * and I have no idea what you are trying to do here:

 `` `` ``int whline(WINDOW * win, chtype ch, int m);``

Oh yes; and endwin(); ends curses mode which in essence quits ncurses and printf just dumps stuff to the terminal after you have exited ncurses mode.

And; you need to use printw() rather than printf() when you are in curse mode.

What you have done here is initialised ncurses then exited it and then you are outputting to the standard terminal, so you have no hope of drawing a line whatsoever.

Finally, don't use std::cout in ncurses mode or bad things will happen.

Basically, sorry dude, but there are more things wrong with this code than right with it.

Last edited on
Perhaps this might help:

 ``123456789101112131415161718192021222324252627`` ``````#include int main() { //Initialise curses mode initscr(); //get the width of the terminal int maxx = getmaxx(stdscr); for(int i =0; i < 20; ++i) { //Print whatever you want here mvprintw(i, 0, "Line: %d", i / 2 +1); ++i; //Then draw a line mvhline(i, 0, ACS_HLINE, maxx); } //Paint the buffer to the terminal window refresh(); //Pause until a key is pressed getch(); //End curses mode endwin(); }``````

Make sure that you are using a terminal font that knows what a line is and make sure you link to ncurses when compiling

g++ myfile.cpp -Incurses -lncurses

If you want a line top and bottom only then you could do something like
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #define HEADER 3 int main() { //Initialise curses mode initscr(); //get the width of the terminal int maxx = getmaxx(stdscr); mvprintw(0, 3, "** Numerical systems **"); mvprintw(1, 1, "\"10\"\t\"8\"\t\"16\"\t\"2\""); mvhline(2, 0, ACS_S3, maxx); for(int i =0; i < 10; ++i) { mvprintw(HEADER + i, 0, "Line: %d", i +1); } mvhline(HEADER +10, 0, ACS_S9, maxx); //Paint the buffer to the terminal window refresh(); //Pause until a key is pressed getch(); //End curses mode endwin(); }``````
Last edited on
Topic archived. No new replies allowed.