-3

好的,所以我需要一个代码来询问日期并将它们保存在一个数组中,格式是 (DAY HH:MM:SS) 我必须使用类所以这里是代码:

主程序

typedef TempsSet Setmana[50];

struct Dies {
    Setmana t;
    int n;
};

int main() {
    Dies e;
    int valors;
    cout << "QUANTS VALORS TENS (>0):" << endl;
    cin >> valors;
    cout << "ENTRA ELS VALORS:" << endl;
    e.n=0;
    for(int n=0;n<valors;n++){
        e.t[n].llegir();
        e.n++;
    }
    cout << "-------------------" << endl;
    for(int n=0;n<valors;n++)
        e.t[n].mostrar();
    cout << "ELS VALORS ORDENATS:";
    e.n=0;
     /*for(int n=0;n<valors;n++)
        e.t[n].mostrar();*/
    return 0;
}

TEMPSSET.CPP

TempsSet::TempsSet() {
    a_d = "DL"; a_h = 0; a_m = 0; a_s = 0;
}
TempsSet::TempsSet(string d, int h, int m, int s) {
    a_d = d; a_h = h; a_m = m; a_s = s;
    if (d!="DL"||d!="DILLUNS"||d!="DT"||d!="DIMATRS"||d!="DC"||d!="DIMECRES"||d!= "DJ"||d!="DIJOUS"||d!="DV"||d!="DIVENDRES"||d!="DS"||d!="DISSABTE"||d!="DG"||d!="DIUMENGE"||h<0||m<0||m>=60||s<0||s>=60)
        a_d = "DL"; a_h = 0; a_m = 0; a_s = 0;
}
void TempsSet::llegir() {
    char c;
    do {
        cin >> a_d;
        cin.ignore(1); // o cin >> c;
        cin >> a_h;
        cin.ignore(1); // o cin >> c;
        cin >> a_m;
        cin.ignore(1); // o cin >> c;
        cin >> a_s;
    } while(a_d!="DL"||a_d!="DILLUNS"||a_d!="DT"||a_d!="DIMATRS"||a_d!="DC"||a_d!="DIMECRES"||
            a_d!="DJ"||a_d!="DIJOUS"||a_d!= "DV"||a_d!="DIVENDRES"||a_d!="DS"||a_d!="DISSABTE"||
            a_d!="DG"||a_d!="DIUMENGE"||a_h<0||a_m<0||a_m>59||a_s<0||a_s>59);
}

该程序询问我要输入多少个日期并将数字保存在“valors”,但它并没有停止要求我介绍日期。如果我说我只介绍 1 也没关系。这可能是 TempsSet.cpp 中的一段时间,我猜有些东西编码错误

4

1 回答 1

1

尝试对有效字符串使用数组。
我改变了你的循环检查:

void TempSet::llegir()
{
    static const char * a_d_strings[] =
        {
            {"DILLUNS"},
            {"DT"},
            {"DIMATRS"},
            {"DC"},
            {"DIMECRES"},
            {"DJ"},
            {"DIJOUS"},
            {"DV"},
            {"DIVENDRES"},
            {"DS"},
            {"DISSABTE"},
            {"DG"},
            {"DIUMENGE"},
        };
    static const unsigned int STRING_QUANTITY =
        sizeof(a_d_strings) / sizeof(a_d_strings[0]);

    bool    invalid_input = true;
    while (invalid_input)
    {
        cin >> a_d;
        cin.ignore(1); // o cin >> c;
        cin >> a_h;
        cin.ignore(1); // o cin >> c;
        cin >> a_m;
        cin.ignore(1); // o cin >> c;
        cin >> a_s;
// Here you should convert a_d to all uppercase.
// Search SO for "transform toupper" to find out how.


       unsigned int i = 0;
        for (i = 0; i < STRING_QUANTITY; ++i)
        {
            if (a_d == a_d_strings[i])
            {
                break;
            }
        }
        if (i >= STRING_QUANTITY)
        {
            cout << "Invalid text. Enter data again.\n";
            continue;
        }
        if (a_h < 0)
        {
            cout << "Invalid hours. Enter data again.\n";
            continue;
        }
        if ((a_m < 0) || (a_m > 59))
        {
            cout << "Invalid minutes.  Enter data again.\n";
            continue;
        }
        if ((a_s < 0) || (a_s > 59))
        {
            cout << "Invalid seconds.  Enter data again.\n";
            continue;
        }
        invalid_data = false;
    }
}
于 2013-02-28T18:03:06.587 回答