不间断循环要求字符串

时间:2013-02-28 17:10:22

标签: c++ string loops

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

的main.cpp

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中的时间,我想有些东西是错误的编码

1 个答案:

答案 0 :(得分: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;
    }
}