Member 12677556 Ответов: 3

Что не так с моим дефолтом?


Привет! У нас есть этот школьный проект, который требует от нас создать программу, в которой мы должны предложить пользователям ввести дату и месяц в числах, результаты которых зависят от введенной даты. На самом деле моя программа работает просто отлично, за исключением того факта, что мой оператор по умолчанию также выводит сам себя, даже если ввод пользователя не является недопустимым. Можете ли вы помочь мне определить ошибку в моем операторе switch и сказать, что не так с тем, как я использовал оператор default? Мне очень жаль, что я не говорю на языке программирования и я просто новичок.

Вот моя программа:

Что я уже пробовал:

#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;
}

3 Ответов

Рейтинг:
6

Patrice T

Ваше утверждение switch неверно
оператор switch в C[^]
оператор switch (C)[^]
Случай переключения в C и C++ - Cprogramming.com[^]
С - переключателя оператор case в языке Си с примерами[^]

Вам нужно 1 switch(m) для всех case и default как описано в ссылках.

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:
...


Рейтинг:
24

OriginalGriff

То, что вы сделали, недопустимо как оператор switch:

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;
}
Это множество отдельных независимых операторов switch, и каждый из них будет выполняться один за другим.
То, что вы хотели, было:
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;
        }
    }


Рейтинг:
14

W∴ Balboos, GHB

Видеть Оператор переключения C++ [^] поскольку ваши операторы switch довольно запутаны и должны выглядеть более похожими:

switch(m) {
 case 1:  something;
          break; 
 case 2:  something;
          break; 
 case 3:  something;
          break; 
 default: something;
          break; 
}