1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
WORD getInput(LPSTR &input) {
HANDLE hStdOut, hStdIn;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
LPSTR lpszPrompt = (LPSTR)"Enter a side of the triangle: ";
CHAR chBuffer[1];
CHAR chInput[256];
DWORD cRead, cWritten;
WORD wCharacters = 0;
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
// Get Console Screen Buffer Information (size, cursor pos, etc.)
if (!GetConsoleScreenBufferInfo(hStdOut, &csbiInfo)) {
MessageBox(NULL, TEXT("GetConsoleScreenBufferInfo"),
TEXT("Console Error"), MB_OK | MB_ICONERROR);
exit(1);
}
if (!WriteFile(hStdOut, lpszPrompt,lstrlenA(lpszPrompt),
&cWritten, NULL)) {
MessageBox(NULL, TEXT("WriteFile"), TEXT("Console Error"),
MB_OK | MB_ICONERROR);
exit(1);
}
while (ReadFile(hStdIn, chBuffer, 1, &cRead, NULL) &&
(chBuffer[0] != '\r' || chBuffer[0] != '\n')) {
wCharacters += cRead;
csbiInfo.dwCursorPosition.X = csbiInfo.dwSize.X-(1+wCharacters);
if (csbiInfo.dwCursorPosition.X == 0 || wCharacters>255)
break;
if (!SetConsoleCursorPosition(hStdOut, csbiInfo.dwCursorPosition)) {
MessageBox(NULL, TEXT("SetConsoleCursorPosition"),
TEXT("Console Error"), MB_OK | MB_ICONERROR);
exit(1);
}
if (!WriteFile(hStdOut, chBuffer, cRead,
&cWritten, NULL)) break;
chInput[wCharacters-1] = chBuffer[0];
}
csbiInfo.dwCursorPosition.X = 0;
if (!SetConsoleCursorPosition(hStdOut, csbiInfo.dwCursorPosition)) {
MessageBox(NULL, TEXT("SetConsoleCursorPosition"),
TEXT("Console Error"), MB_OK | MB_ICONERROR);
exit(1);
}
strcpy(input, chInput);
return wCharacters;
}
int main() {
LPSTR chText;
getInput(chText);
printf("\n%s", chText);
exit(0);
}
|