Good day. I am trying to write a simple program that will substitute other values ​​from the source text. The problem is that the program replaces the values ​​of only the first five elements and, if they are written together without punctuation marks, adding Latin letters, for example, generally crashes the program. I tried to increase the value of the variable that is responsible for replacing the elements, but the problem is solved 50 to 50. It is painted the same way. Only a string of fused letters is greater. Source code below. Just lay out the whole project. Maybe it will be easier. https://yadi.sk/d/nGIOLDqB35vc7y

#include <Windows.h> #include"resource.h" HWND hEdit1; HWND hEdit2; const int m = 1000, n = 5; CHAR str[m] = { 0 }; CHAR rezult[] = { 0 }; CHAR buttons[n] = { 'X','Y','A','B' }; BOOL CALLBACK DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) { DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, (DlgProc), 0); return 0; } BOOL CALLBACK DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: { hEdit1 = GetDlgItem(hwnd, IDC_EDIT1); SetWindowText(hEdit1, str); SetFocus(hEdit1); hEdit2 = GetDlgItem(hwnd,IDC_EDIT2); SetWindowText(hEdit2, rezult); break; } case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON1: GetWindowText(hEdit1, str, 255); for (int i = 0; i<n; ++i) if (str[i] > '0' && str[i] < m + '0') { str[i] = buttons[str[i] - '0' - 1]; } SetWindowText(hEdit2, str); break; case IDOK: MessageBox(hwnd, "OK", "Info", MB_OK || MB_ICONINFORMATION); break; case IDCANCEL: EndDialog(hwnd, 0); return FALSE; } break; case WM_CLOSE: EndDialog(hwnd, 0); return FALSE; } return FALSE; } 
  • What is this m + '0'? - Vlad from Moscow
  • As far as I know an array like with 0 starts counting. And I need 1 = X, 2 = Y, 3 = A, 4 = B. - Petr

1 answer 1

This cycle

  for (int i = 0; i<n; ++i) if (str[i] > '0' && str[i] < m + '0') { str[i] = buttons[str[i] - '0' - 1]; } 

makes no sense. Since the variable m has a value of 1000

 const int m = 1000, n = 5; ^^^^^^^^ 

then, in fact, this condition

 if (str[i] > '0' && str[i] < m + '0') 

will be executed for any str[i] value greater than '0' . However, the correct range of indices for the array of buttons , declared as

 CHAR buttons[n] = { 'X','Y','A','B' }; 

this is just [0, 3] . Thus, for any value of str[i] that is greater than '4' going beyond the array will occur, which leads to unspecified program behavior.

  • It turns out you need to remove the limit on the array itself? Or just remove the extra variable from the program? - Petr
  • @Petr First, you have a loop from 0 to 4 for (int i = 0; i <n; ++ i), so only 5 initial characters of the string are checked. And, secondly, you need to ensure that the index in the buttons array does not go beyond it. - Vlad from Moscow
  • Strange. I applied the exact same array in the program in the console view and everything worked fine there. Thanks for the tip. I will continue to torment the code :) - Petr
  • @Petr Keep in mind that you may have UNICODE enabled, and therefore you must deal with "wide" characters. - Vlad from Moscow
  • In the project settings is the use of multibyte encoding. - Petr