| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 521 人关注过本帖
标题:大家帮忙看看要怎么解决这个问题
只看楼主 加入收藏
surongre
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-2-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
大家帮忙看看要怎么解决这个问题
自己编了个计算器,可以运算乘法和求园的面积。然后编译时报错。请问要怎么办?
源码:
程序代码:
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <stdio.h>
#include <stdlib.h> 

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
        HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }

    return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
        TCHAR str1[256];
        TCHAR str2[256];
        GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
        GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
        int i1=atoi(str1);
        int i2=atoi(str2);
        int i4=i1*i2;
        TCHAR str4[256];
        wsprintf(str4,"%i",i4);
        SetDlgItemText(hwnd,IDC_EDIT3,str4);
        break;
    }

        case IDC_OK2:
        TCHAR str3[256];
        GetDlgItemText(hwnd,IDC_EDIT4,str3,sizeof(str3));
        int i3=atoi(str3);
        int i5=i3*i3*3.14;
        wsprintf(str3,"%i",i5);
        SetDlgItemText(hwnd,IDC_EDIT4,str3);
}

void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}


报错信息:
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\MainDlg.cpp(44) : error C2046: illegal case
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\MainDlg.cpp(48) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
执行 cl.exe 时出错.

请问要怎么办?
搜索更多相关主题的帖子: 计算器 
2011-02-27 17:07
jxp97365280
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-2-23
收藏
得分:0 
如鹏网的。。。?

秋风扫遍了一个个小程序!
2011-02-27 17:10
surongre
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-2-26
收藏
得分:0 
回复 2楼 jxp97365280
恩。怎么了?
2011-02-27 17:11
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:15 
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <stdio.h>
#include <stdlib.h>

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
        HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }

    return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
        TCHAR str1[256];
        TCHAR str2[256];
        GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
        GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
        int i1=atoi(str1);
        int i2=atoi(str2);
        int i4=i1*i2;
        TCHAR str4[256];
        wsprintf(str4,"%i",i4);
        SetDlgItemText(hwnd,IDC_EDIT3,str4);
        break;
        case IDC_OK2:
        TCHAR str3[256];
        GetDlgItemText(hwnd,IDC_EDIT4,str3,sizeof(str3));
        int i3=atoi(str3);
       int i5=(int)(i3*i3*3.14);  //第二个错误在这里,怎么解决要看你的意思?
        wsprintf(str3,"%i",i5);
        SetDlgItemText(hwnd,IDC_EDIT4,str3);
        break;
    }  //括号写错地方了吧?
}

void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}




[ 本帖最后由 qq1023569223 于 2011-2-27 17:18 编辑 ]
收到的鲜花
  • surongre2011-02-27 18:08 送鲜花  2朵   附言:谢谢解决问题

   唯实惟新 至诚致志
2011-02-27 17:15
surongre
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-2-26
收藏
得分:0 
程序代码:
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <stdio.h>
#include <stdlib.h> 

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
        HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }

    return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
        TCHAR str1[256];
        TCHAR str2[256];
        GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
        GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
        int i1=atoi(str1);
        int i2=atoi(str2);
        int i4=i1*i2;
        TCHAR str4[256];
        wsprintf(str4,"%i",i4);
        SetDlgItemText(hwnd,IDC_EDIT3,str4);
    }
        switch(id) //加上switch语句就可以运行
    {
        case IDC_OK2:
        TCHAR str3[256];
        GetDlgItemText(hwnd,IDC_EDIT4,str3,sizeof(str3));
        int i3=atoi(str3);
        int i5=(int)(i3*i3*3.14); //输出的是整数怎么办?
        wsprintf(str3,"%i",i5);
        SetDlgItemText(hwnd,IDC_EDIT5,str3);
        break;
    }
}

void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}
这样就可以编译通过了...............

但是求面积那步,在IDC_EDIT5输出(i3*i3*3.14)的结果怎么没有小数点?

[ 本帖最后由 surongre 于 2011-2-27 17:32 编辑 ]
2011-02-27 17:30
犬虫门心
Rank: 8Rank: 8
来 自:西安
等 级:蝙蝠侠
帖 子:209
专家分:753
注 册:2011-1-25
收藏
得分:5 
int i5=(int)(i3*i3*3.14); //输出的是整数怎么办?
int是整型量,楼主又把乘积结果进行了强制类型转换,不是整型数,还能是什么呢?
用double吧。

当一名对得起学生学费的老师,一直是我的目标!我会更努力的!
2011-02-27 18:42
format123
Rank: 2
等 级:论坛游民
帖 子:35
专家分:60
注 册:2010-11-26
收藏
得分:0 
看看你的switch函数,括号的配对错了,还有就是将i5定义为double
2011-02-27 22:00
快速回复:大家帮忙看看要怎么解决这个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017632 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved