[求助]救命啊~~~~~~~
这个程序哪里不对劲了啊#include<iostream.h>
class Date
{
public:
void SetDate();
void Next();
private:
int year;
int month;
int day;
int days;
};
void Date::SetDate()
{
cout<<"Enter the year,month,day:";
cin>>year>>month>>day;
for(;year<0;)
{
cout<<"Enter a wrong year.\n";
cin>>year;
}
for(;month<0||month>0;)
{
cout<<"Enter a wrong month\n";
cout<<"Enter the month:";
cin>>month;
}
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days=31;break;
case 4:
case 6:
case 9:
case 11:days=30;break;
case 2:if((year%4==0&&year%100!=0)||(year%400==0))
days=28;
else days=28;
}
void Date::Next(int year,int month,int day)//第二天
{
if(month=12&&day=days)//年底
++year;
month=1;
day=1;
else if(month!=12&&day=days)//月底
++month;
day=1;
else ++day;
cout<<"The next day is "<<day<<"/"month<<"/"<<year;
}
int main()
{
Date ymd;
ymd.SetDate();
ymd.Next();
return 0;
} 偶告诉你哪里不对劲吧……就是偶完全看不懂……真不对劲…… ........................ for(;month<0||month>0;)
month无论小于0或大于0都会执行该循环,是个死循环。
只有month=0时才跳出循环。
但month=0时,switch语句没有执行到一个case,
days会成为没有初始化的变量,
在Next()方法里引用一个没有初始化的变量是不行的。
为了程序的健壮性,对day也应进行输入合法性判断。
大概就这些吧。 好象有N多个问题~ Originally posted by hjack at 2004-10-21 17:26:
for(;month<0||month>0;)
month无论小于0或大于0都会执行该循环,是个死循环。
只有month=0时才跳出循环。
但month=0时,switch语句没有执行到一个case,
days会 ...
呵呵~这个是笔误啦
不过很谢谢你
这个我打算重搞了
说说你编译有什么错误?
还有 ymd.Next();要有参数的。 给你一个正确的程序吧:
// a.cpp : 定义控制台应用程序的入口点。
//
#include \"stdafx.h\"
#include<iostream>
using namespace std;
class Date
{
public:
void SetDate();
void Next(int,int ,int);
public:
int year;
int month;
int day;
int days;
};
void Date::SetDate()
{
cout<<\"Enter the year,month,day:\";
cin>>year>>month>>day;
for(;year<0;)
{
cout<<\"Enter a wrong year.\\n\";
cin>>year;
}
for(;month<0||month>12;)
{
cout<<\"Enter a wrong month\\n\";
cout<<\"Enter the month:\";
cin>>month;
}
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days=31;break;
case 4:
case 6:
case 9:
case 11:days=30;break;
case 2:if((year%4==0&&year%100!=0)||(year%400==0))
days=28;
else days=28;
}
}
void Date::Next(int year,int month,int day)//第二天
{
if(month==12&&day==days)//年底
{
++year;
month=1;
day=1;
}
else if(month!=12&&day==days)//月底
{
++month;
day=1;
}
else ++day;
cout<<\"The next day is \"<<day<<\"/\"<<month<<\"/\"<<year;
}
int _tmain(int argc, _TCHAR* argv[])
{
Date ymd;
ymd.SetDate();
ymd.Next(ymd.year,ymd.month,ymd.day);
char c=getchar();
while(c!=\'e\')
c=getchar();
return 0;
}
我在vc.net环境下运行通过了,实现了你要的功能。
你还可以扩展一下,在处理异常方面做得更好些。
自己慢慢体会。。。。 喔~~~~~~~~
谢谢高人指点!! char c=getchar();
while(c!=\'e\')
c=getchar();
请问这个有什么作用的啊?
在vc.net环境下?什么意思啊?
还是不行啊,它说“Cannot open include file: \'stdafx.h\' 在visual c++.net下,你要先建一个项目,为win 32控制台项目。(你用的是什么编译环境?)
char c=getchar();
while(c!=\'e\')
c=getchar();
是为了查看结果,只有当输入字符e才退出程序。
如果你用VC6.0的话,上面的程序不要#include"stdafx.h"应该可以运行的吧。 是VC6.0的
不行啊
痛苦~~~~~ vc6.0下把#include"stdafx.h"去掉,
把主函数改为int main()就行啦。
多试试。。。 死循环啊~~~~~~~
超痛苦~~~~
简直想死~ 不可能的,我在vc 6.0下都可以通过。
结果为:
Enter the year,month,day:2004 10 25
The next day is:26/10/2004
那里死循环???
这样做一定不会有错误了:
进入vc6.0,文件->新建->C++ Source file.
建一个cpp文件,输入:
#include<iostream>
using namespace std;
class Date
{
public:
。。。。//省略了,自己添上
int main()
{
Date ymd;
ymd.SetDate();
ymd.Next(ymd.year,ymd.month,ymd.day);
char c=getchar();
while(c!=\'e\')
c=getchar();
return 0;
}
编译-执行。
ok 哦~~~~~~~
我输入错了
不过好像很不完善呢
跟想象中的差点了
呵呵`~~~~~~~~~
不过还是很谢谢你啊 我前面都说了,我这是按你的程序改的,
我已叫你对异常进行捕获,扩展一下它。
其实这个程序还有很多不足的,你自己扩展一下对自己很有帮助的。 呵呵~~~~~
知道了
等我不会这么痛苦的时候在研究它吧
页:
[1]