0

数字 8 と 9 を除いて、デフォルトは機能しません。10 以降では、最初の整数を使用し、その後の 2 番目の数字を完全に無視するケースとして扱います。助けてください

#include <iostream> 
#include <string>
using namespace std;

int main ()
{
     char day;
        cout << " Enter day of the week " << endl;
        cin >> day;
        switch (day)
   {  
        case '1' : case '6' : case '7' :
           cout << "weekend";
            break;

    case '2' : case '4' :
        cout << "going to C++ Class";
        break;

    case '3' : case '5' :
        cout << "studying for C++ Class";
        break;

    default :
        cout << "invalid day number";


}


    system("pause");
    return 0;

}

4

5 に答える 5

1

dayとして宣言されているため、変数に 1 文字しか格納していませんchar。に変更しint、switch ステートメントのケースをint値に変更します。

#include <iostream> 
#include <string>
using namespace std;

int main ()
{
    int day;
    cout << " Enter day of the week " << endl;
    cin >> day;

    switch (day)
    {  
        case 1:
        case 6:
        case 7:
            cout << "weekend";
            break;
        case 2:
        case 4:
            cout << "going to C++ Class";
            break;
        case 3:
        case 5:
            cout << "studying for C++ Class";
            break;
        default :
            cout << "invalid day number";
    }

    system("pause");
    return 0;
}
于 2013-10-09T02:08:50.090 に答える