Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aurimas13

macrumors newbie
Original poster
Aug 18, 2017
2
0
Hello everyone, I have been searching on 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 someone of a help. I need to draw two lines: one above Numerical systems and the other beneath 10, 8, 16 and 2 in the code below. Also please explain how to draw a line vertically using ncurses. I am struggling to understand how to draw lines using ncurses in mac:(

Here's my code (whline two commands of 0 and 3 are not working):

Code:
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <ncurses.h>
#include <curses.h>

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 by a moderator:
Horizontal line : __________________________

Vertical line:
|
|
|
|
|
|
|

That's it. Just place the curser at the location where you want to draw a "|" or a "_" and draw it. Draw enough of them and you have a line.
 
Last edited:
After curses is initialized, the screen is then under the control of curses. You shouldn't use C++'s console output operator (<<) any more. Use of printf would also be wrong. You should do all terminal I/O using the appropriate curses functions. Curses maintains a "virtual screen", and direct console output interferes with the link between the virtual screen and the actual screen. Curses then has an incorrect view of the actual screen.

After properly init'ing curses, you must use only curses functions for all screen or keyboard access. If you don't know how to init curses correctly, start by reading the curses(3X) man page.

I didn't have any trouble finding curses examples (search terms: curses examples). If you can't find any, please post your search terms.

If you find examples that don't work, post the URL of the example, and describe exactly what didn't work;
1. Be specific.
2. Post your code.
3. Describe what you expected to happen.
4. Describe what actually happened.​

Of course, as pointed out in post #2, you can just draw lines yourself using ordinary characters. If you do that, then don't use any curses functions at all. You should either do all the display I/O with curses, or all without curses. Mixing them is a mistake.
 
Thanks chown33. I made a mistake whilst mixing I/O with curses. I'll now read more about it and will tell if I have any questions afterwards. Thanks again!
 
Also I'd like to point out that most of that code is absolute garbage. And that's not an insult, we've all been there. The conversion to binary stuff could have been a single function and you could just print the function's result. For more guidance do a Google search for "printf in binary" or something like that. Hardcoding 15 possible cases is bad practice defined :D
And please don't use an ostringstream as a simple buffer. A plain C++ string can do the same, or in this (bad example!) case even a char[4] array would be enough.

Also, at one point in that code, you already have the string "result" available. Let's forget about the horrible way you got it. You can easily output the string! Instead you're parsing it with stoi() as a decimal integer (which it isn't) and printing it as a decimal integer (essentially converting it back into a string).

Also instead of this
Code:
cout << "\"10\"" << "\t" << "\"8\"" << "\t" << "\"16\"" << "\t" << "\"2\"" << endl;
you can just use this
Code:
cout << "\"10\"\t\"8\"\t\"16\"\t\"2\"" << endl;
I know it doesn't look that good, but every << is in fact a rather expensive function call. It doesn't matter at all in this example, but you should never waste CPU cycles just because you can. That's what interpreted languages are for, not C++ :D
 
  • Like
Reactions: jweinraub
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.