How best to write this code? The goal - learning improvement.
Code:
#include <windows.h>
#define STRLEN(x) (sizeof(x)/sizeof(TCHAR) - 1)
const TCHAR szMsg[] = L"What's your name?\n";
void ChangeTextColor(HANDLE hSomeHandle) {
INT nArgs = 0;
LPWSTR lpCommandLine = GetCommandLine();
LPWSTR* lpArgs = CommandLineToArgvW(lpCommandLine, &nArgs);
if(nArgs >= 2 && 0 == lstrcmpi(lpArgs[1], L"green")) {
SetConsoleTextAttribute(hSomeHandle, FOREGROUND_GREEN);
}
LocalFree(lpArgs);
}
int main() {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
TCHAR szName[16];
TCHAR szResp[32];
DWORD dwCount = 0;
ChangeTextColor(hStdout);
WriteConsole(hStdout, &szMsg, STRLEN(szMsg), &dwCount, NULL);
ReadConsole(hStdin, &szName, STRLEN(szName), &dwCount, NULL);
if(dwCount >= 2 &&
'\n' == szName[dwCount-1] &&
'\r' == szName[dwCount-2]) {
szName[dwCount-2] = '\0';
} else if(dwCount > 0) {
szName [dwCount]= '\0';
}
wsprintf(szResp, L"Hello, %s!\n", szName);
WriteConsole(hStdout, &szResp, lstrlen(szResp), &dwCount, NULL);
ExitProcess(0);
}
Помилки
Code:
C: \ API \ Hello \ Hello.cpp | 4 | error: character array initialization string of wide characters |
C: \ API \ Hello \ Hello.cpp | 8 | error: cannot convert «LPSTR {aka char *}» to «LPWSTR {aka wchar_t *}» in initialization |
C: \ API \ Hello \ Hello.cpp | 10 | error: cannot convert «LPWSTR {aka wchar_t *}» to «LPCSTR {aka const char *}» for argument «1» to «int lstrcmpiA (LPCSTR, LPCSTR)» |
C: \ API \ Hello \ Hello.cpp | 36 | error: cannot convert «const wchar_t *» to «LPCSTR {aka const char *}» for argument «2» to «int wsprintfA (LPSTR, LPCSTR, ...) »|
|| === Build failed: 4 error (s), 0 warning (s) (0 minute (s), 3 second (s)) === |