问关于c ++开关和程序

时间:2014-12-12 13:59:17

标签: c++ if-statement switch-statement

嗨我试图做一个程序,从用户检查日期是否正确 并以月为单位返回日期 如果日期不正确,程序应退出 但是当我输入数字时,它什么都没有 请帮助我

{
    int day,month,year;

    cout<<"enter day\n";
    cin>>day;
    cout<<"enter month\n";
    cin>>month;
    cout<<"enter year\n";
    cin>>year;
    if ( (   (((month==1)|| (month==3)|| (month==5)|| (month==7)|| (month==8)|| (month==10)|| (month==12) )&&((day<32)&&(day>0)))      ||      (((month==4)|| (month==6)|| (month==9)|| (month==11))&&((day<31)&&(day>0)))  ||  ((month==2)&&((day<29)&&(day>0)))  ) && ((year>-1)&&(year<3001))   )
     {
          switch(month)
          {
               case 1:
                   cout<<day<<"/"<<"january/"<<year;
                   break;
               case 2:
                   cout<<day<<"/"<<"febuary/"<<year;
                   break;

               case 3:
                   cout<<day<<"/"<<"march/"<<year;
                   break;

               case 4:
                    cout<<day<<"/"<<"april/"<<year;
                    break;

               case 5:
                    cout<<day<<"/"<<"may/"<<year;
                    break;

               case 6:
                    cout<<day<<"/"<<"june/"<<year;
                    break;

               case 7:
                   cout<<day<<"/"<<"july/"<<year;
                   break;

               case 8:
                   cout<<day<<"/"<<"August/"<<year;
                   break;

               case 9:
                   cout<<day<<"/"<<"September/"<<year;
                   break;

               case 10:
                   cout<<day<<"/"<<"October/"<<year;
                   break;

               case 11:
                   cout<<day<<"/"<<"November/"<<year;
                   break;

               case 12:
                   cout<<day<<"/"<<"December/"<<year;
                   break;

               default;
                   cout<<"not correct";
          }

      }
      else 
      {
          cout<<"false";
      }

      return 0;
};

2 个答案:

答案 0 :(得分:0)

它甚至不应该编译 - “默认;”应该是“默认:”。

如果这是固定的,并且被适当的#include包围,使用命名空间std(yuk)和int main(int argc,char ** argv),那么它会编译并输出一些内容。

但是有一些错误。

C:\>D:\Projects\temp\Debug\temp.exe
enter day
30
enter month
12
enter year
2000
30/December/2000not correct
C:\>

答案 1 :(得分:0)

您可以将数组用于动态且易于理解的代码

int day,month,year;

std::string months [] = { "january" , "febuary" , "march" , "april" , "may" , "june" , "july" , "August" , "September" , "October" , "November" , "December" } ;

int total_day [12];
total_day [0] = 31;
total_day[1] = 29;
total_day[2] = 31;
total_day[3] = 30;
total_day[4] = 31;
total_day[5] = 30;
total_day[6] = 31;
total_day[7] = 31;
total_day[8] = 30;
total_day[9] = 31;
total_day[10] = 30;
total_day[11] = 31;

std::cout<<"enter day :\n";
std::cin>>day;
std::cout<<"enter month :\n";
std::cin>>month;
std::cout<<"enter year :\n";
std::cin>>year;

if ( (month > 0 && month <= 12) && (day > 0 && day <= total_day[month-1]) && (year > -1 && year < 3001))
{
    std::cout << day << "/"<< months[month-1] << "/" <<year;
}
else
{
    std::cout << "Wrong Date";
}