Thread: How best to write this code? The goal - learning improvement.

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    74

    How best to write this code? The goal - learning improvement.

    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)) === |

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You didn't tell the compiler that you're compiling in UNICODE mode.

    Working with Strings (Windows)
    You can't just put TCHAR and L"string" and hope for the best.
    For example, you need to write
    const TCHAR szMsg[] = TEXT("What's your name?\n");

    > wsprintf(szResp, L"Hello, %s!\n", szName);
    > WriteConsole(hStdout, &szResp, lstrlen(szResp), &dwCount, NULL);
    Routine Mappings
    The 'C' API functions (say strlen) have mapped names (like _tcslen) to enable the same code to work with either the old ANSI API, or the modern UNICODE API.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would just use

    int main(int argc, const char** argv)

    and then use std::cout. No need to mess with the Windows API for this. You only need that to change the console colour.
    Also use std::wstring if you're going to deal with unicode on Windows, at least to start out with (otherwise use std::string). Don't use C arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    Дякую! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Improvement help - Criticise my code
    By Click_here in forum C Programming
    Replies: 21
    Last Post: 08-28-2012, 08:51 AM
  2. Replies: 2
    Last Post: 11-01-2011, 01:57 PM
  3. code improvement before i go further!!
    By samirself in forum C Programming
    Replies: 2
    Last Post: 06-24-2005, 11:52 PM
  4. Code improvement suggestions
    By Xzyx987X in forum C Programming
    Replies: 16
    Last Post: 03-26-2004, 12:03 PM
  5. goal is to write a telephone directory
    By cowgiljl in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2003, 03:21 AM

Tags for this Thread