• Welcome to Valhalla Legends Archive.
 

[C/++] Very Simple Console Program (Input/Output Flow Help)

Started by rabbit, May 13, 2007, 07:39 PM

Previous topic - Next topic

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.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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:

#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:



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.

Warrior

Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Skywing

If you're using Win32, you might check out the console manipulation APIs like ReadConsoleOutput and ScrollConsoleScreenBuffer.

rabbit

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.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

K

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.

Newby

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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

Banana fanna fo fanna


K

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 :)

Newby

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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

K

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.

Newby

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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.