我的默认有什么问题? [英] What's wrong with my default?

查看:95
本文介绍了我的默认有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我们有这个学校项目,要求我们创建一个程序,我们需要提示用户输入日期和月份,其输出取决于输入的日期。实际上我的程序运行得很好,除非我的默认语句也输出自己,即使用户的输入无效。你能帮我识别我的switch语句中的错误,并告诉我如何使用default语句有什么问题吗?对不起,我不会说编程语言而且我只是个新手。



这是我的计划:



我的尝试:



Hi! We have this school project which requires us to create a program wherein we need to prompt the users to enter a date, and month in numbers, whose outputs depend on the date entered. Actually My program runs just fine except for the fact that my default statement also outputs itself even though the user's input is not invalid. Can you help me identify the error in my switch statement and tell me what's wrong with how I used the default statement? I'm sorry I don't speak programming language and I'm just a newbie.

Here's my program:

What I have tried:

#include <iostream>
#include <string>


using namespace std;

int main ()
{
    int d = 0;
    int m = 0;

    cout<<"Please enter the date today:"<<endl;
    cout<<"Day: ";
    cin>>d;
    cout<<endl;
    cout<<"Month: ";
    cin>>m;
    cout<<endl;

    string months [] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    string days [] = {"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24tth","25th","26th","27th","28th","29th","30th","31st"};

    switch(m)

case 1:
    {
    if (d == 1)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"New Year is here!"<<endl;
    }
    else if (d <= 31)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
    else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
switch (m)
case 2:
    {
    if (d == 8)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Chinese New Year is here!"<<endl;
    }

    else if (d == 14)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Valentine's Day is here!"<<endl;
    }
    else if (d <= 28)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 3:
    {
    if (d == 24)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Maundy Thursday is here!"<<endl;
    }
    else if(d == 25)
    {
        cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Good Friday is here!"<<endl;
    }
    else if (d == 26)
    {
         cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Holy Saturday is here!"<<endl;
    }
        else if (d <= 31)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
 break;
    }
    switch (m)
case 4:
    {
    if (d == 9)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"The Day of Valor is here!"<<endl;
    }

    else if (d <= 30)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }

    break;
    }
    switch (m)
case 5:
    {
    if (d == 5)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Lailatul Isra Wal Mi Raj is here!"<<endl;
    }

    else if (d <= 31)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }

    break;
    }
    switch (m)
case 6:
    {
    if (d == 12)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Independence Day is here!"<<endl;
    }
    else if (d == 20)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"June Solistice is here!"<<endl;
    }
    else if (d <= 30)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 7:
    {
    if (d <= 31)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Speical Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 8:
    {
       if (d == 21)
       {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Ninoy Aquino Day is here!"<<endl;
       }
       else if (d == 29)
       {
       cout<<"Hi!"<<endl;
      cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"National Hereos Day is here!"<<endl;
       }
       else if (d <= 31)
       {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
       }
        else
       {
       cout<<"That is an invalid input."<<endl;
       }
    break;
    }
    switch (m)
case 9:
    {
    if (d == 22)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"September Equinox is here!"<<endl;
    }
     else if (d <= 30)
       {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
       }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }

    break;
    }
    switch (m)
case 10:
    {
    if (d <= 31)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 11:
    {
    if (d == 1)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"Halloween is here!"<<endl;
    }
    else if (d == 2)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"All Souls' Day is Here!"<<endl;
    }

    else if (d == 30)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"Bonifacio Day is here!"<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 12:
    {
    if (d == 21)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"December Solistice is here!"<<endl;

    }
    else if (d == 24)
    {
    cout<<"Hi!"<<endl;
   cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"Christmas Eve is here!"<<endl;
    }
    else if (d == 25)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"Christmas Day is here!"<<endl;
    }
    else if (d == 30)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"Rizal Day is here!"<<endl;
    }
    else if (d == 31)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"New Year's Eve is here!"<<endl;
    }
    else if (d <= 29)
    {
    cout<<"Hi!"<<endl;
    cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
    cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }

    switch (m)
default:
    {
       cout<<"That is an invalid input."<<endl;
    break;
    }
return 0;
}

推荐答案

What you’ve go isn’t valid as a switch statement:

What you've go isn't valid as a switch statement:
switch(m)
case 1:
{
...
break;
}
switch (m)
case 2:
{
...
break;
}
switch (m)
case 3:
{
...
break;
}
switch (m)
case 4:
{
...
break;
}
switch (m)
case 5:
{
...
break;
}
switch (m)
case 6:
{
...
break;
}
switch (m)
case 7:
{
...
break;
}
switch (m)
case 8:
{
...
break;
}
switch (m)
case 9:
{
...
break;
}
switch (m)
case 10:
{
...
break;
}
switch (m)
case 11:
{
...
break;
}
switch (m)
case 12:
{
...
break;
}
switch (m)
default:
{
...
break;
}

That is loads of individual, independent switch statements, and each will be executed one after the other.

What you wanted was:

That is loads of individual, independent switch statements, and each will be executed one after the other.
What you wanted was:

switch(m)
    {
    case 1:
        {
        ...
        break;
        }
    case 2:
        {
        ...
        break;
        }
    case 3:
        {
        ...
        break;
        }
    case 4:
        {
        ...
        break;
        }
    case 5:
        {
        ...
        break;
        }
    case 6:
        {
        ...
        break;
        }
    case 7:
        {
        ...
        break;
        }
    case 8:
        {
        ...
        break;
        }
    case 9:
        {
        ...
        break;
        }
    case 10:
        {
        ...
        break;
        }
    case 11:
        {
        ...
        break;
        }
    case 12:
        {
        ...
        break;
        }
    default:
        {
        ...
        break;
        }
    }


See C++ switch statement[^] as your switch statements are quite a mess and should look more like:

See C++ switch statement[^] as your switch statements are quite a mess and should look more like:
switch(m) {
 case 1:  something;
          break; 
 case 2:  something;
          break; 
 case 3:  something;
          break; 
 default: something;
          break; 
} 


Your switch statement is wrong

switch statement in C[^]

switch Statement (C)[^]

Switch Case in C and C++ - Cprogramming.com[^]

C - switch case statement in C language with example[^]



You need 1 switch(m) for all case and the default as described in links.

Your switch statement is wrong
switch statement in C[^]
switch Statement (C)[^]
Switch Case in C and C++ - Cprogramming.com[^]
C - switch case statement in C language with example[^]

You need 1 switch(m) for all case and the default as described in links.
switch(m)
 {
case 1:
    {
    if (d == 1)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"New Year is here!"<<endl;
    }
    else if (d <= 31)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
    else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
switch (m)
case 2:
    {
    if (d == 8)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Chinese New Year is here!"<<endl;
    }
 
    else if (d == 14)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"Valentine's Day is here!"<<endl;
    }
    else if (d <= 28)
    {
       cout<<"Hi!"<<endl;
       cout<<"It is the "<<days[d-1]<<" of "<<months[m-1]<<"!"<<endl;
       cout<<"No Special Event Today."<<endl;
    }
     else
    {
       cout<<"That is an invalid input."<<endl;
    }
    break;
    }
    switch (m)
case 3:
...


这篇关于我的默认有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆