Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: rabbit on May 13, 2007, 07:39 PM

Title: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: rabbit on May 13, 2007, 07:39 PM
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.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: K on May 13, 2007, 09:04 PM
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:

#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;
   
}



And the appearance would be:

(http://ucsu.colorado.edu/~ledbettj/curses-3.png)

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 (http://msdn2.microsoft.com/en-us/library/ms682073.aspx).
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Warrior on May 13, 2007, 09:12 PM
No.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: rabbit on May 14, 2007, 10:11 AM
ncurses is a pain to install, but I'll try it.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Skywing on May 14, 2007, 10:14 AM
If you're using Win32, you might check out the console manipulation APIs like ReadConsoleOutput and ScrollConsoleScreenBuffer.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: rabbit on May 14, 2007, 10:35 AM
A problem I have is this
while(true)
{
    output_stuff();
    scanf("%s", in_buf);
}
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.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Newby on May 15, 2007, 10:16 PM
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.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: K on May 15, 2007, 10:42 PM
Quote from: Newby on May 15, 2007, 10:16 PM
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.

You know that I posted that first, right?  ::)
Don't give him too much credit for being smart or clever.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Newby on May 15, 2007, 11:11 PM
Quote from: K on May 15, 2007, 10:42 PM
Quote from: Newby on May 15, 2007, 10:16 PM
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.

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

I knew it was too good to be true.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Banana fanna fo fanna on May 16, 2007, 03:55 PM
Is there an edit or deleted post I missed?
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: K on May 16, 2007, 04:52 PM
Quote from: Banana fanna fo fanna on May 16, 2007, 03:55 PM
Is there an edit or deleted post I missed?

No.  tumeria and Newby copied my post, so used internet voodoo to slightly modify the screenshots you can find in their posts :)
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Newby on May 16, 2007, 06:45 PM
Quote from: K on May 16, 2007, 04:52 PM
No.  tumeria and Newby copied my post, so used internet voodoo to slightly modify the screenshots you can find in their posts :)

Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: K on May 16, 2007, 07:13 PM
Quote
Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.

He certainly did post first, but he edited his useless post to mirror mine.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Newby on May 16, 2007, 07:35 PM
Quote from: K on May 16, 2007, 07:13 PM
Quote
Maybe there's something horridly wrong with my settings, but tumeria's post came first in this thread.

He certainly did post first, but he edited his useless post to mirror mine.

Ahhhh. I didn't catch there. And here I thought you were just having fun. I'll edit mine now.
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Banana fanna fo fanna on May 16, 2007, 07:53 PM
ohhhh ok
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: brew on May 17, 2007, 08:31 PM
could you ban squeak's ip range plz
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: warz on May 18, 2007, 12:42 AM
or C++
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: rabbit on May 18, 2007, 07:07 PM
Hey, who wants to lock/split this thread?
Title: Re: [C/++] Very Simple Console Program (Input/Output Flow Help)
Post by: Zakath on May 20, 2007, 12:06 AM
No need, methinks. :)