1
votes

I am developing an MFC Interface with Visual Studio, but the output is not as it should. I am using the same code as the on used in codeblocks but the output here is different and i think it's because of the format. What is the correct way to enter 'e' and 'd' in my 'IDC_Values' ? I searched online but couldn't find much about MFC

int e[100], d[100];

CString Text;
Text.Format((LPCWSTR)L"%d \t%d", e, d);
SetDlgItemText(IDC_Values, Text);
2
e and d are arrays, so printing them with the %d format specifier is wrong. What are you trying to achieve? Print the 1st element of the arrays? Print all elements of the arrays? Also you don't needd the (LPCWSTR) cast before the format string. What is IDC_Values? The id of an edit control, of a static or...? Show an example of values in eand in d and the text you want to put in IDC_Values (what ever control IDC_Values is). - Jabberwocky
I want to print all elements of the array - Ben
IDC_Values is the id of an edit control - Ben
As to the LPCWSTR cast -- never cast string types, even though it may "work". If a string can't live by itself without casts being applied, the code could and should be considered wrong. Casting is a sign that you're trying to shut the compiler up about an error about differing string types. Instead, use the right type of string for the job. - PaulMcKenzie
You need to write a loop to print all values of the array. Even so, how would Format know how to format a series of numbers? A space in-between them? Two spaces? Carriage return? A pipe symbol? - PaulMcKenzie

2 Answers

3
votes

CString is CStringW on UNICODE builds and CStringA on non UNICODE builds. So you should not mix wide literals with non wide, for example you have:

Text.Format((LPCWSTR)L"%d \t%d", e, d);
                     ^ ~~~~ this requires that CString is wide

this should be (minus the fact that e and d are arrays!!):

Text.Format(_T("%d \t%d"), e, d);

Now if you want to format a string, and set it to widget, then you must iterate it (warning: I have not compiled this code):

CString Text;
CString tmp;
for (size_t i = 0; i < sizeof(e)/sizeof(e[0]); ++i) {
  tmp.Format(_T("%d,"), e[i]);
  Text += tmp;
}

// here the same for d

I am not saying its the most efficent way.

0
votes

If you want to print the content of an array you must iterate the array and build the string I guess you want a list having rows and columns containg e[] and d[]? I assume that e. d are filled completely. If so you need to code like

int e[100], d[100];
CString Text;
CString Line
for(int i=0;i<100;i++){
    Line.Format((LPCWSTR)L"%d \t%d\r\n", e, d);
    Text+=Line;
}
SetDlgItemText(IDC_Values, Text);

This will generate a multiline string containing the values of e and d. If this is what you want.