C++ 不停循环请求字符串
好的,我需要一个代码来请求日期并将它们保存在数组中,格式是DAY HH:MM:SS,我必须使用类,下面是代码: MAIN.CPPC++ 不停循环请求字符串,c++,string,loops,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; ci
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中,我猜有些编码错误请尝试使用数组作为有效字符串。 我更改了您的循环检查:
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;
}
}
所以问题在哪里?考虑到你问题的性质,这太多代码了。试着去适应变量、函数的英文代码名,@Ergo Proxy,问题是当我说我只想要X时,为什么它不停止要求我介绍日期。在南诺里,编辑了代码,对不起,太多了,你是对的。在LihO,我不能使用英语变量,因为字符串是在纠正这些类型的程序中设置的,它们必须是完全相同的字符串。另外,我住在西班牙,我不能用另一种语言输入带有变量的代码。请尝试使用小写输入逐步完成函数,然后再次使用相同但大写的文本。我真的认为表数组搜索更容易阅读和维护。