我正在构建一个程序,该程序旨在将星期几/值对中的值存储到向量中,然后显示和汇总一周中每一天的值。因此,我要求用户为每个cin输入输入一个字符串(一周中的一天,一周中的每一天可以采用4种不同的形式)和一个整数(对应的值)。 (该程序仅显示星期一和到目前为止的退出条件;我稍后将其扩展为包括星期二至星期日。)
该程序还用于识别错误的输入(并允许用户重试其输入)。但是,如果仅正确输入星期几,我很难让程序识别错误的输入。例如,如果输入“ test test”作为输入,则程序将成功宣布“检测到星期几错误”。如果我输入“ Monday x”,它也会宣布此消息(即使需要调整措词)。但是,如果我输入“测试5”,则程序会接受此操作而不会显示“星期几错误”消息。
如何更改程序,以便在输入“测试5”之类的内容时使用预先存在的else语句显示“星期几错误”?
一种解决方案是创建一个很长的if语句,如果输入的星期几与29个有效的星期几中的任何一个都不匹配(例如“星期一”,“星期一”,“星期一”,“ ”,“星期一”,“星期二”,“星期二” ...)。但是,我想找到一种更简单的方法。
谢谢您的帮助!
#include "../std_lib_facilities.h"
#include <iostream>
//Program in progress. Completing as part of my independent study of Programming: Principles and Practice by Bjarne Stroustrup.
int main()
{
string dayofweek;
int value;
vector<int> mondayvalues;
vector<int> tuesdayvalues;
vector<int> wednesdayvalues;
vector<int> thursdayvalues;
vector<int> fridayvalues;
vector<int> saturdayvalues;
vector<int> sundayvalues;
int incorrectentrycounter = 0;
int mondaysum = 0;
cout << "Please enter days of the week followed by values in integer form. When you are finished, please enter Done 0.\n";
string incorrectnumdump;
while (cin) {
if (cin >> dayofweek >> value) {
if (dayofweek == "Monday" || dayofweek == "monday" || dayofweek == "Mon" || dayofweek == "mon") {
mondayvalues.push_back(value);
}
if ((dayofweek == "Done") && (value == 0)) {
break;
}
}
else {
cin.clear();
cin >> incorrectnumdump;
cout << "Incorrect day of week detected; please try again.\n";
incorrectentrycounter++;
continue;
}
}
cout << "Here are the values you entered for each day of the week, along with their sums:\n";
cout << "Monday: ";
for (int x : mondayvalues)
cout << x << " ";
for (int i = 0; i < mondayvalues.size(); i++) {
mondaysum += mondayvalues[i];
}
cout << "\nSum of Monday values: " << mondaysum;
cout << "\nThere were " << incorrectentrycounter << "entries that displayed non-valid days of the week.";
}
嗯。也许您可以使用ASCII技术将所有输入内部翻转为全部大写字母或所有小写字母,这样,只要大写字母或小写字母没有,您就不必在if语句中查找这两种情况。没关系。使函数接受输入并将其转换为全部大写或全部小写。在将输入分配给变量之前,请使用此功能。这将使检查它们更加容易。在那之后,您可以创建一个std :: string的const数组[7],其中将包括所有的日子。您可以通过消除方法来检查输入。您将在以后逐个比较输入天数和数组天数,并消除每次都不匹配的天数。如果消除了所有日子,则输入错误。如果还剩2天或以上-输入的信息不足。如果还有1天,那是正确的输入!让我知道您是否需要帮助
[在研究了代码之后,我决定使用此解决方案,我认为它将满足我的需求。该代码现在具有两个无效的entry语句块,而不是一个。第一个(“检测到星期中的错误日期”)在if (cin >> dayofweek >> value)
条件内,它将标识与星期一字符串或“完成”之一都不匹配的星期几条目。
第二个(“无效条目”)在if (cin >> dayofweek >> value)
条件之外,但在while (cin)
条件内。我添加了第二个代码,以便如果有人输入类似“ test test”的内容,则将通知他们错误,但while循环仍将继续。
因此,似乎用户需要通过cin >>输入多个值时,一种输入验证方法是在代码的不同级别输入多个错误消息。
此解决方案仍然有些笨拙,但允许我仅使用“ else” {}而不是“ else”来捕获无效条目,然后再输入一系列条件。
#include "../std_lib_facilities.h"
#include <iostream>
//Program in progress. Completing as part of my independent study of Programming: Principles and Practice by Bjarne Stroustrup.
int main()
{
string dayofweek;
int value;
vector<int> mondayvalues;
vector<int> tuesdayvalues;
vector<int> wednesdayvalues;
vector<int> thursdayvalues;
vector<int> fridayvalues;
vector<int> saturdayvalues;
vector<int> sundayvalues;
int incorrectentrycounter = 0;
int mondaysum = 0;
cout << "Please enter days of the week followed by values in integer form. When you are finished, please enter Done 0.\n";
string incorrectnumdump;
while (cin)
{
if (cin >> dayofweek >> value)
{
if (dayofweek == "Monday" || dayofweek == "monday" || dayofweek == "Mon" || dayofweek == "mon")
{
mondayvalues.push_back(value);
}
else if ((dayofweek == "Done") && (value == 0))
{
break;
}
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "Incorrect day of week detected; please try again.\n";
incorrectentrycounter++;
continue;
}
}
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "Invalid entry; please try again.\n";
incorrectentrycounter++;
continue;
}
}
cout << "Here are the values you entered for each day of the week, along with their sums:\n";
cout << "Monday: ";
for (int x : mondayvalues)
cout << x << " ";
for (int i = 0; i < mondayvalues.size(); i++)
{
mondaysum += mondayvalues[i];
}
cout << "\nSum of Monday values: " << mondaysum << "\n";
cout << "\nThere were " << incorrectentrycounter << " invalid entries.";
}