void CJiangxuejin::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
int sum, all = 0, max = 0;
CString str,result;
int i;
char str1[1000];
char name[100], topname[100];
int  chengji, pingyi, lunwen;
char ganbu, xibu;
while(n--)
{
GetDlgItem(IDC_EDIT2)->GetWindowText(str);
sum = 0;
for( i = 0; i < str.GetLength(); ++i)
        str1[i] = str[i];
str1[i+1] = '\0';
sscanf(str1, "%s %d %d %c %c %d", name, &chengji, &pingyi, &ganbu, &xibu, &lunwen);
if(chengji > 80 && lunwen >= 1) sum += 8000;
if(chengji > 85 && pingyi > 80) sum += 4000;
if(chengji > 90) sum += 2000;
if(chengji > 85 && xibu == 'Y') sum += 1000;
if(pingyi > 80 && ganbu == 'Y') sum += 850;
if(sum > max)
{
max = sum;
strcpy(topname, name);
}
all += sum;
}
MessageBox(_T("数据处理完毕"));
result.Format(_T("%s\n\r%d\n\r%d\n\r"), topname, max, all);
    GetDlgItem(IDC_EDIT3)->SetWindowText(result);}为什么输出是乱码啊。

解决方案 »

  1.   

    for( i = 0; i < str.GetLength(); ++i)
            str1[i] = str[i];
    str1[i+1] = '\0'; for( i = 0; i < str.GetLength(); i++)
            str1[i] = str[i];
    str1[i] = '\0'; sscanf(str1, "%s %d %d %c %c %d", name, &chengji, &pingyi, &ganbu, &xibu, &lunwen); 
    看看name是什么
      

  2.   

    for( i = 0; i < str.GetLength(); ++i) 
            str1[i] = str[i]; 
    str1[i+1] = '\0';
    一个是宽字节,一个是窄字节
    怎么能赋值
    cstring在unicode下是宽字节的
      

  3.   

    把里面的char的字符串换成TCHAR应该就好了
      

  4.   

    看样子不是VC6.0
    VS2003以上版本默认使用Unicode编码方式
      

  5.   

    我是为了用sscanf才想把CString str 变成char str1【1000】的。
    请问我要怎么才能达到目的呢?
      

  6.   

    sscanf(str1, "%s %d %d %c %c %d", name, &chengji, &pingyi, &ganbu, &xibu, &lunwen); 
    改为sscanf(str1, L"%s %d %d %c %c %d", name, &chengji, &pingyi, &ganbu, &xibu, &lunwen); 
    试试
      

  7.   

    sscanf(str1, "%s %d %d %c %c %d", name, &chengji, &pingyi, &ganbu, &xibu, &lunwen); 改为 _stscanf(str1,_T("%s %d %d %c %c %d"), name, &chengji, &pingyi, &ganbu, &xibu, &lunwen); 
      

  8.   

    同时把 str2,name之类的 类型 换成 TCHAR[]