QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1481|回复: 6

我编的一个小程序,switch语句错误,请人帮忙解决

[复制链接]
发表于 2003-11-26 08:53:47 | 显示全部楼层 |阅读模式
程序代码:
#include <iostream>
using namespace std;

int main()
{
        int year,month,day;
        int i,j=28,days=0;
       
        cout&lt;&lt;"Input date(yyyy mm dd):";
        cin&gt;&gt;year;
        cin&gt;&gt;month;
        cin&gt;&gt;day;
        if((year/100)&amp;&amp;!(year/4)||year/400)
                   j=29;
                  

          switch (month)
                  case 1:
                  case 3:
                  case 5:
                  case 7:
                  case 8:
                  case 10:
                  case 12:i=31;
                          break;
                  case 2:i=j;
                    break;
                  case 4:
            case 6:
             case 9:
              case 11:i=30;
                      break;
              default:{
                      cout&lt;&lt;"Fault date:month"&lt;&lt;endl;
                       return 1;
        }

           if ((day&gt;i)||(day&lt;1))
    {
            cout&lt;&lt;"Fault date:days"&lt;&lt;endl;
            return 1;
           }
             
        switch (month)
                case 1:days=day;break;
                case 2:days=31+day;break;
                case 3:days=31+j+day;break;
                case 4:days=31+j+31+day;break;
                case 5:days=31+j+31+30+day;break;
                case 6:days=31+j+31+30+31+day;break;
                case 7:days=31+j+31+30+31+30+day;break;
                case 8:days=31+j+31+30+31+30+31+day;break;
                case 9:days=31+j+31+30+31+30+31+31+day;break;
                case 10:days=31+j+31+30+31+30+31+31+30+day;break;
                case 11:days=31+j+31+30+31+30+31+31+30+31+day;break;
                case 12:days=31+j+31+30+31+30+31+31+30+31+30+day;break;
               
        cout&lt;&lt;"days="&lt;&lt;days&lt;&lt;endl;
       
        return 0;
}


编译错误报告
g++.exe "D:\yzf\c\lx2.cpp" -o "D:\yzf\c\lx2.exe"    -I"D:\yzf\Dev-Cpp\include\c++"  -I"D:\yzf\Dev-Cpp\include\c++\mingw32"  -I"D:\yzf\Dev-Cpp\include\c++\backward"  -I"D:\yzf\Dev-Cpp\include"   -L"D:\yzf\Dev-Cpp\lib"
D:/yzf/c/lx2.cpp: In function `int main()':
D:/yzf/c/lx2.cpp:26: case label `2' not within a switch statement

D:/yzf/c/lx2.cpp:28: case label `4' not within a switch statement
D:/yzf/c/lx2.cpp:29: case label `6' not within a switch statement
D:/yzf/c/lx2.cpp:30: case label `9' not within a switch statement
D:/yzf/c/lx2.cpp:31: case label `11' not within a switch statement
D:/yzf/c/lx2.cpp:33: `default' label not within a switch statement
D:/yzf/c/lx2.cpp:46: case label `2' not within a switch statement
D:/yzf/c/lx2.cpp:47: case label `3' not within a switch statement
D:/yzf/c/lx2.cpp:48: case label `4' not within a switch statement
D:/yzf/c/lx2.cpp:49: case label `5' not within a switch statement
D:/yzf/c/lx2.cpp:50: case label `6' not within a switch statement

D:/yzf/c/lx2.cpp:51: case label `7' not within a switch statement
D:/yzf/c/lx2.cpp:52: case label `8' not within a switch statement
D:/yzf/c/lx2.cpp:53: case label `9' not within a switch statement
D:/yzf/c/lx2.cpp:54: case label `10' not within a switch statement
D:/yzf/c/lx2.cpp:55: case label `11' not within a switch statement
D:/yzf/c/lx2.cpp:56: case label `12' not within a switch statement
D:/yzf/c/lx2.cpp:25: break statement not within loop or switch
D:/yzf/c/lx2.cpp:27: break statement not within loop or switch
D:/yzf/c/lx2.cpp:32: break statement not within loop or switch
D:/yzf/c/lx2.cpp:45: break statement not within loop or switch
D:/yzf/c/lx2.cpp:46: break statement not within loop or switch
D:/yzf/c/lx2.cpp:47: break statement not within loop or switch
D:/yzf/c/lx2.cpp:48: break statement not within loop or switch
D:/yzf/c/lx2.cpp:49: break statement not within loop or switch
D:/yzf/c/lx2.cpp:50: break statement not within loop or switch
D:/yzf/c/lx2.cpp:51: break statement not within loop or switch
D:/yzf/c/lx2.cpp:52: break statement not within loop or switch
D:/yzf/c/lx2.cpp:53: break statement not within loop or switch
D:/yzf/c/lx2.cpp:54: break statement not within loop or switch
D:/yzf/c/lx2.cpp:55: break statement not within loop or switch
D:/yzf/c/lx2.cpp:56: break statement not within loop or switch

执行结束
 楼主| 发表于 2003-11-26 09:02:39 | 显示全部楼层
我找到问题了,少了{}
回复

使用道具 举报

发表于 2003-11-27 17:24:30 | 显示全部楼层
我看你很有必要改进一下程序风格

怪恐怖啊,偶都怕怕了 T_T
回复

使用道具 举报

发表于 2003-11-27 18:22:54 | 显示全部楼层
是应该把风格给改一改才对的!
回复

使用道具 举报

发表于 2003-12-30 10:50:30 | 显示全部楼层
式有点恐怖!!!!!!!!!!!!
回复

使用道具 举报

发表于 2004-2-10 22:34:08 | 显示全部楼层
该怎么改进呢?新手同问
回复

使用道具 举报

发表于 2004-2-11 06:39:30 | 显示全部楼层
缩进,还有二元操作符两边加空格,注释。。。
最好看一下编码规范,养成好习惯。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-9 02:06 , Processed in 0.090267 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表