2013-02-28 37 views
-3

好了,所以我需要一个询问日期,并利用数组保存它们的编码,格式为(DAY HH:MM:SS),我不得不所以这里使用类的代码:Nonstopping循环要求的字符串

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,虽然我认为是错误编码

+3

那么,问题在哪里? – 2013-02-28 17:13:15

+0

考虑到你的问题的性质,这是太多的代码来阅读。采取一切无所谓 – 2013-02-28 17:14:38

+2

尝试习惯英文代码(变量名称,功能,...) – LihO 2013-02-28 17:17:45

回答

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