注册 登录
编程论坛 C++教室

[已解决]switch哪里出错了。。

kyan54 发布于 2007-06-30 10:36, 1005 次点击

定义一个满足如下要求的Date类
1。用日/月/年的格式输出日期
2。可运行在日期上加一天操作
3。设置日期操作


#include<iostream>
using namespace std;
class Date{
int year;
int month;
int day;
public:
void data(int d,int m,int y){
year=y;month=m;day=d;
}
void print(){
cout<<day<<" | "<<month<<" | "<<year<<endl;
}
void add(){
day+=1;
switch(day) /*这里主要是判断天数,遇到29.28.30.31会当月数加1*/
case 29:if(month==2 || year/4==0&&year/100!=0||year/400==0)
{ day=1;month+=1;}
case 28:if(month==2)
{day=1;month+=1;}
case 30:if(month==4 || month==6 || month==9 || month==11)
{ day=1;month+=1; }
case 31:if(month==1 || month==3 || month==5 || month==7 || month==8 ||month==10)
{ day=1;month+=1;}
else if(month==12)
{day=1;month=1;year+=1;}
}

};

void main()
{
int y,m,d;
cout<<"day/month/year"<<endl;
cin>>d>>m>>y;

Date p;
p.data(d,m,y);
p.print();
p.add();
p.print();
}


illegal case运行出来3个这个错误。。
我看不懂这是什么错误。。。

大家帮帮忙。。先谢过了


[此贴子已经被作者于2007-6-30 11:26:22编辑过]

4 回复
#2
kyan542007-06-30 11:05

#include<iostream>
using namespace std;
class Date{
int year;
int month;
int day;
public:
void data(int d,int m,int y){
year=y;month=m;day=d;
}
void print(){
cout<<day<<" | "<<month<<" | "<<year<<endl;
}
void add(){
switch(month)
case 1:if(day==30)
{ day=1;month+=1; }
break;
case 2:if(year/4==0&&year/100!=0||year/400==0 && day==29)
{ day=1;month+=1;}
else if(day==28)
{ day=1;month+=1;}
break;
case 3:if(day==31)
{ day=1;month+=1;}
break;
case 4:if(day==30)
{ day=1;month+=1;}
break;
case 5:if(day==31)
{ day=1;month+=1;}
break;
case 6:if(day==30)
{ day=1;month+=1;}
break;
case 7:if(day==31)
{ day=1;month+=1;}
break;
case 8:if(day==31)
{ day=1;month+=1;}
break;
case 9:if(day==30)
{ day=1;month+=1;}
break;
case 10:if(day==31)
{ day=1;month+=1;}
break;
case 11:if(day==30)
{ day=1;month+=1;}
break;
case 12:if(day==31)
{ day=1;month+=1;}
break;
default:day+=1;break;
}

};

void main()
{
int y,m,d;
cout<<"day/month/year"<<endl;
cin>>d>>m>>y;

Date p;
p.data(d,m,y);
p.print();
p.add();
p.print();
}



我把switch里的变量了成month还是错的。。。


--------------------Configuration: Date - Win32 Debug--------------------
Compiling...
Date.cpp
H:\C++\Date.cpp(18) : error C2043: illegal break
H:\C++\Date.cpp(19) : error C2046: illegal case
H:\C++\Date.cpp(23) : error C2043: illegal break
H:\C++\Date.cpp(24) : error C2046: illegal case
H:\C++\Date.cpp(26) : error C2043: illegal break
H:\C++\Date.cpp(27) : error C2046: illegal case
H:\C++\Date.cpp(29) : error C2043: illegal break
H:\C++\Date.cpp(30) : error C2046: illegal case
H:\C++\Date.cpp(32) : error C2043: illegal break
H:\C++\Date.cpp(33) : error C2046: illegal case
H:\C++\Date.cpp(35) : error C2043: illegal break
H:\C++\Date.cpp(36) : error C2046: illegal case
H:\C++\Date.cpp(38) : error C2043: illegal break
H:\C++\Date.cpp(39) : error C2046: illegal case
H:\C++\Date.cpp(41) : error C2043: illegal break
H:\C++\Date.cpp(42) : error C2046: illegal case
H:\C++\Date.cpp(44) : error C2043: illegal break
H:\C++\Date.cpp(45) : error C2046: illegal case
H:\C++\Date.cpp(47) : error C2043: illegal break
H:\C++\Date.cpp(48) : error C2046: illegal case
H:\C++\Date.cpp(50) : error C2043: illegal break
H:\C++\Date.cpp(51) : error C2046: illegal case
H:\C++\Date.cpp(53) : error C2043: illegal break
H:\C++\Date.cpp(54) : error C2047: illegal default
H:\C++\Date.cpp(54) : error C2043: illegal break
执行 cl.exe 时出错.

Date.obj - 1 error(s), 0 warning(s)

#3
天下第二刀2007-06-30 11:18
switch 少一对括号
#4
天下第二刀2007-06-30 11:20
这种小问题自己要学会能找~~
   以后还会有更多这样的问题
#5
kyan542007-06-30 11:26
饿。。。知道了。。。谢谢~~
1