Thread: Help editing console output

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Help editing console output

    I'm having a question about timers and console windows:

    is there a simple way to make a timer in a console window while the user is inputing text into the window?

    I want to accept input from a user while updating the console window, like, if you have this design:


    Seconds: <ammount of seconds>

    Input: <user inputing>


    How am i supposed to update the ammount of seconds without removing what the user is inputing?

    Thanks for any replies...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's possible, but not using standard C library functions, as the standard only provides for moving within the same line and down, never up.

    Timers are also system dependant.

    As to how you get the system to accept input and also perform the timer is yet another system defendant feature that isn't part of standard C.

    Are you using a Windows OS, or something else?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Yes, I'm using Windows Vista and Visual C++ compiler.

    I'm only planning to run this program on windows computers, so system dependant functions aren't a problem...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you will need to use console functions to read input and manage cursor positions:
    Console Functions (Windows)

    You will either need a second thread, or some sort of timing function built into your input loop. Can't say what is best wihtout knowing MORE about your ultimate goal with the program.

    You may be able to use WaitForSingleObject() and pass in the return from GetStdHandle(STD_INPUT_HANDLE).
    WaitForSingleObject: WaitForSingleObject Function (Windows)

    GetStdHandle: GetStdHandle Function (Windows)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Ok, just to give an example of what I want: you probably know the code input in the LOST series? Say I wanted to display that same timer and wait for the input of those same numbers, does that make the problem clearer? I also want to update the timer every second, not every minute...

    Basically, I want a timer counting down every second while waiting for a specific input, and maintaining the user input while updating the timer (making the system("clr") impossible to use).

    Some sort of example code for this kind of application or at least some structure for an application would be appreciated...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and you can do that with the functions I've given.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    I managed to make a program like the LOST example i mentioned, here is the code

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <time.h>
    
    HANDLE wHnd;    // Handle to write to the console.
    HANDLE rHnd;    // Handle to read from the console.
    
    bool numbers = false;
    
    void WINAPI NewThread();
    
    int main()
    {
    	DWORD dwID;
    	HANDLE hThread;
    	int input1, input2, input3, input4, input5, input6;
    
        // Set up the handles for reading/writing:
        wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
        rHnd = GetStdHandle(STD_INPUT_HANDLE);
    
        // Change the window title:
        SetConsoleTitle(L"LOST");
    
        // Set up the required window size:
        SMALL_RECT windowSize = {0, 0, 79, 49};
        
        // Change the console window size:
        SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
    
        
        // Create a COORD to hold the buffer size:
        COORD bufferSize = {80, 50};
    
        // Change the internal buffer size:
        SetConsoleScreenBufferSize(wHnd, bufferSize);
    
    	hThread = CreateThread(
    		0,
    		NULL,
    		(LPTHREAD_START_ROUTINE)NewThread,
    		NULL,
    		NULL,
    		&dwID);
    
    	printf("\n\n");
    
    	while (TRUE)
    	{
    		scanf("%d%d%d%d%d%d", &input1, &input2, &input3, &input4, &input5, &input6);
    		if(input1 == 4 && input2 == 8 && input3 == 15 && input4 == 16 && input5 == 23 && input6 == 42)
    			numbers = true;
    	}
    }
    
    void WINAPI NewThread()
    {
        CHAR_INFO consoleBuffer[1*4];
    
        char tempBuffer[6];
    
    	sprintf(tempBuffer, "%d", 108*60);
    
    	consoleBuffer[0].Char.UnicodeChar = tempBuffer[0];
    	consoleBuffer[0].Attributes = BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
    	consoleBuffer[1].Char.UnicodeChar = tempBuffer[1];
    	consoleBuffer[1].Attributes = BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
    	consoleBuffer[2].Char.UnicodeChar = tempBuffer[2];
    	consoleBuffer[2].Attributes = BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
    	consoleBuffer[3].Char.UnicodeChar = tempBuffer[3];
    	consoleBuffer[3].Attributes = BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
    
        // Set up the positions:
        COORD charBufSize = {4,1};
        COORD characterPos = {0,0};
        SMALL_RECT writeArea = {0,0,3,0}; 
    
    	time_t start = time(NULL);
    
    	start = start+(108*60);
    
    	while (TRUE)
    	{
    		WriteConsoleOutput(wHnd, consoleBuffer, charBufSize, characterPos, &writeArea);
    
    		sprintf(tempBuffer, "%d", start-time(NULL));
    
    		consoleBuffer[0].Char.UnicodeChar = tempBuffer[0];
    		consoleBuffer[1].Char.UnicodeChar = tempBuffer[1];
    		consoleBuffer[2].Char.UnicodeChar = tempBuffer[2];
    		consoleBuffer[3].Char.UnicodeChar = tempBuffer[3];
    
    		Sleep(100);
    
    		if (numbers == true)
    		{
    			start = time(NULL)+(108*60);
    			numbers = false;
    		}
    	}
    }
    Probably not the easiest way of doing it, but it works perfectly.

    Thanks for all help

    I know it's not good to have infinite loops, but it works for now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get the console output
    By outlawbt in forum Windows Programming
    Replies: 2
    Last Post: 03-19-2008, 02:25 PM
  2. redirect console output to a String?
    By TwistedMetal in forum C Programming
    Replies: 10
    Last Post: 02-26-2008, 02:54 PM
  3. Help with Console output
    By JeremyCAFE in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2005, 10:36 AM
  4. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  5. Redirecting console output
    By _hannes in forum Windows Programming
    Replies: 3
    Last Post: 11-04-2004, 04:51 AM