Made a mistake in the new code that I realized now, here is the new code.
Code:
#define SYSTEM WINDOWS
#include <process.h>
#include <windows.h>
#else
printf("You are using a invalid computer for this program!");
exit(EXIT_FAILURE);
#endif // _WIN32
void cls( HANDLE hConsole );
void clearstdinbffr(void);
void GetInput(void *param)
{
char UserCommand;
do
{
scanf("%1c", &UserCommand);
clearstdinbffr();
} while (tolower(UserCommand) != 'q');
exit(EXIT_SUCCESS);
_endthread();
}
void PrintOutput(void *param)
{
int o;
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
for ( o = 0; o<INFINITE; o++ )
{
printf("Woohoo!");
Sleep(1000);
cls(hStdout);
}
_endthread();
}
int main() // <------------------ Main entry point is here
{
HANDLE loop_thread[2]; // Declares an array for 2 processes
loop_thread[0] = (HANDLE) _beginthread( GetInput, 0, NULL ); // Begins thread 1
if ( loop_thread[0] == INVALID_HANDLE_VALUE ) exit( EXIT_FAILURE ); // Exits with a failure if thread
// was not created properly
loop_thread[1] = (HANDLE) _beginthread( PrintOutput, 0, NULL ); // Begins thread 2
if ( loop_thread[1] == INVALID_HANDLE_VALUE ) exit( EXIT_FAILURE ); // Same error as bfore
WaitForMultipleObjects(2, loop_thread, TRUE, INFINITE); // Wait for the threads to complete before exiting with 0
return 0;
}
void cls( HANDLE hConsole ) // Credit for this function goes to Microsoft's website's help section here :
{ // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682022%28v=vs.85%29.aspx
COORD coordScreen = { 0, 0 }; // home for the cursor
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the number of character cells in the current buffer.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire screen with blanks.
if( !FillConsoleOutputCharacter( hConsole, // Handle to console screen buffer
(TCHAR) ' ', // Character to write to the buffer
dwConSize, // Number of cells to write
coordScreen, // Coordinates of first cell
&cCharsWritten ))// Receive number of characters written
{
return;
}
// Get the current text attribute.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
// Set the buffer's attributes accordingly.
if( !FillConsoleOutputAttribute( hConsole, // Handle to console screen buffer
csbi.wAttributes, // Character attributes to use
dwConSize, // Number of cells to set attribute
coordScreen, // Coordinates of first cell
&cCharsWritten )) // Receive number of characters written
{
return;
}
// Put the cursor at its home coordinates.
SetConsoleCursorPosition( hConsole, coordScreen );
}
void clearstdinbffr(void) { int c = ' '; while((c = getchar()) != '\n' && c != EOF); }