Valhalla Legends Forums Archive | C/C++ Programming | [C/++] Very Simple Console Program (Input/Output Flow Help)

AuthorMessageTime
rabbit
I'm trying to find a way to have a simple console program run with a scrolling output section and then a single static input line at the bottom.  I'm not sure how it's done.  I was thinking it was probably not iostream or stdio, but I'm not that familiar with either, really.
May 14, 2007, 12:39 AM
K
The easiest way would probably be to use the ncurses library, if there is a windows version (I assume there is).

Here's a little sample program:

[code]#include <ncurses.h>
#include <string.h>

WINDOW* output_window;
WINDOW* input_window;

WINDOW* create_window(int height, int width, int x, int y)
{
  WINDOW* w = newwin(height, width, y, x);
  box(w, 0, 0);

  // now actually create a new window that is one character smaller around
  // the edges than the other window
  wrefresh(w);

  w = newwin(height - 2, width -2, y + 1, x + 1);

  return w;
}

void destroy_window(WINDOW* w)
{
  // erase the bounding box
  wborder(w, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  wrefresh(w);
  delwin(w);
}

#define BUFFER_SIZE 512
char buffer[BUFFER_SIZE];

int main(int argc, char* argv[])
{
  initscr();
 
  output_window = create_window(LINES - 3, COLS, 0, 0);
  input_window  = create_window(3, COLS ,0 , LINES - 3);

  wrefresh(input_window);
  wrefresh(output_window);

  while(true)
  {
      mvwgetnstr(input_window, 0, 0, buffer, BUFFER_SIZE);
      // you should actually just figure out how many characters to print,
      // but i'm lazy..,this mostly clears the input box.
      wprintw(input_window, "                                          ");
     
      if (!strcmp(buffer, "/quit"))
break;
     
      wprintw(output_window, "%s\n", buffer);
     
      wrefresh(output_window);
      wrefresh(input_window);
     
  }

  endwin();

  return 0;
 
}
[/code]


And the appearance would be:

[img]http://ucsu.colorado.edu/~ledbettj/curses-3.png[/img]

ncurses is pretty powerful.  If it seems like overkill or won't do for some reason, you could probably accomplish what you want with the Windows console functions.
May 14, 2007, 2:04 AM
Quarantine
No.
May 14, 2007, 2:12 AM
rabbit
ncurses is a pain to install, but I'll try it.
May 14, 2007, 3:11 PM
Skywing
If you're using Win32, you might check out the console manipulation APIs like ReadConsoleOutput and ScrollConsoleScreenBuffer.
May 14, 2007, 3:14 PM
rabbit
A problem I have is this
[code]while(true)
{
    output_stuff();
    scanf("%s", in_buf);
}[/code]Output only is displayed after the user enters something, but I want output to flow and do its own stuff regardless of whether the user is inputting anything or not.
May 14, 2007, 3:35 PM
Newby
That's good stuff. Hahahaha.

I didn't know you were smart. Behind all that trolling is a sense of humor and a decent ability to program. Incredible.
May 16, 2007, 3:16 AM
K
[quote author=Newby link=topic=16697.msg169099#msg169099 date=1179285365]
That's good stuff. Hahahaha.

I didn't know you were smart. Behind all that trolling is a sense of humor and a decent ability to program. Incredible.
[/quote]

You know that I posted that first, right?  ::)
Don't give him too much credit for being smart or clever.
May 16, 2007, 3:42 AM
Newby
[quote author=K link=topic=16697.msg169100#msg169100 date=1179286975]
[quote author=Newby link=topic=16697.msg169099#msg169099 date=1179285365]
That's good stuff. Hahahaha.

I didn't know you were smart. Behind all that trolling is a sense of humor and a decent ability to program. Incredible.
[/quote]

You know that I posted that first, right?  ::)
Don't give him too much credit for being smart or clever.
[/quote]

I knew it was too good to be true.
May 16, 2007, 4:11 AM
St0rm.iD
Is there an edit or deleted post I missed?
May 16, 2007, 8:55 PM
K
[quote author=Banana fanna fo fanna link=topic=16697.msg169132#msg169132 date=1179348935]
Is there an edit or deleted post I missed?
[/quote]

No.  tumeria and Newby copied my post, so used internet voodoo to slightly modify the screenshots you can find in their posts :)
May 16, 2007, 9:52 PM
Newby
[quote author=K link=topic=16697.msg169134#msg169134 date=1179352378]
No.  tumeria and Newby copied my post, so used internet voodoo to slightly modify the screenshots you can find in their posts :)
[/quote]

Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.
May 16, 2007, 11:45 PM
K
[quote]
Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.
[/quote]

He certainly did post first, but he edited his useless post to mirror mine.
May 17, 2007, 12:13 AM
Newby
[quote author=K link=topic=16697.msg169145#msg169145 date=1179360789]
[quote]
Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.
[/quote]

He certainly did post first, but he edited his useless post to mirror mine.
[/quote]

Ahhhh. I didn't catch there. And here I thought you were just having fun. I'll edit mine now.
May 17, 2007, 12:35 AM
St0rm.iD
ohhhh ok
May 17, 2007, 12:53 AM
BreW
could you ban squeak's ip range plz
May 18, 2007, 1:31 AM
warz
or C++
May 18, 2007, 5:42 AM
rabbit
Hey, who wants to lock/split this thread?
May 19, 2007, 12:07 AM
Zakath
No need, methinks. :)
May 20, 2007, 5:06 AM

Search