Help with console program

This is a discussion on Help with console program within the C++ Programming forums, part of the General Programming Boards category; How can I create a console program that simultaneously handles input from the user, and outputs text? I tried creating ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    13

    Help with console program

    How can I create a console program that simultaneously handles input from the user, and outputs text? I tried creating a sample program with two threads, one basically did something like
    Code:
    while (true) { getline(cin, somestring ); }
    and the other thread randomly slept and wrote to the console. The problem was that when I begain to write input, the output function (which just cout-ed a string) cut off the text I had written so far. I would like behavior that simply "pushes" what I'm writing to the bottom line, and every time I cout something, it will appear above that, so the last line in the console will be reserved for user input, never touched by the output text.

    This can be windows specific, as I am not working with any other operating system.

    Thanks in advance.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Why would you want to do this? With out any context we really can't help you.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This is a little program that outputs a " --- " in every 3 seconds and at the same time it takes input.
    Code:
    #include <iostream>
    #include <windows.h>
    #include <process.h>
    
    void thread(void *P){
    	while(true){
    		Sleep(3000);
    		printf(" --- ");
    	}
    }
    
    int main(){
    	char buffer[256];
    	_beginthread(thread,0,NULL);
    	while(buffer[0]!='*'){
    		std::cin.getline(buffer,256,'\n');
    	}
    	return 0;
    }
    Last edited by maxorator; 10-22-2006 at 11:20 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    13
    Thank you for your replies.

    maxorator, I'm glad that you immediately knew what I wanted , but I have tried that approach and it has a problem. Start the program and type hello, as the "---" outputs it cuts off your input, and if you want to write something long, you won't have enough time.

    The use is for instance a program which gives the user status about something evey once in a while, but allows the user to always input some commands at any time he wants.
    (i.e. a thread starts to do some calculations, but the user can input the commands pause, stop at anytime)

    It is like this in many console programs that I have seen, so it should not be such a problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intergrating console program into windowed app
    By spadez in forum C Programming
    Replies: 4
    Last Post: 02-26-2009, 11:58 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. How to avoid console program crash when terminated too early
    By Xargo in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2007, 04:43 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21