如何使用数组字符串进行输入验证
How to do a input validation with a string of array?
我正在编写一个程序,我必须输入一个月,然后输出该月有多少天。但是,我还必须编写一个代码,如果输入不是有效的月份,它会显示一条错误消息。我无法弄清楚如何输出该错误语句。不过我已经弄清楚了其余的。
我已经尝试了while循环,但它对我不起作用。我知道我一定做错了什么,但我不知道如何解决它。
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int MONTHS = 12;
int days[MONTHS] = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
string m;
string i[MONTHS] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
cout << "Enter the name of the month: " << endl;
cin >> m;
while (m < MONTHS)
{
cout << "Invalid month entered! Exit the program to try again."
<< endl;
}
cout << endl;
for (int count = 0; count < MONTHS; count++)
if (m == i[count])
{
cout << "There are " << days[count] << " days in "
<< i[count] << "." << endl;
}
return 0;
}
每当用户输入无效月份时,这是预期结果。
Input the name of a month: Orion
Orion is not the name of a month.
您的while
循环试图将m
(字符串(与MONTHS
(整数 12(进行比较。与其尝试解决这个问题,我的建议是按照for
循环调整代码。您已经在将m
与数组中的每个月进行比较,对吧?如果m
匹配,则无需继续循环,因此您只需在此时return
即可。然后,如果for
循环在没有匹配的情况下完成,则您知道该月份无效并可以报告它。像这样:
for (int count = 0; count < MONTHS; count++)
if (m == i[count])
{
cout << "There are " << days[count] << " days in "
<< i[count] << "." << endl;
return 0;
}
cout << m " is not the name of a month." << endl;
return 1;
有几个小问题会导致您的错误。
首先是您将字符串m
与 int MONTHS
进行比较。这不会按照你期望的方式工作(可能根本行不通,但至少(。
其次,正如其他人所提到的,你的循环中没有任何东西可以结束循环。您将需要重置 m 的值,这样您就不会得到无限循环。
以下是您可以执行的操作的建议,这些操作应该可以按预期工作:
注意:它需要一个标准::地图
#include <iostream>
#include <string>
#include <map> // if you wanted to do it this way
using namespace std;
int main()
{
const int MONTHS = 12;
int days[MONTHS] = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
string m;
map<string, int> i = { ("January", 0}, "February", 1}, "March", 2},
{"April", 3}, {"May", 4}, {"June", 5}, {"July", 6},
{"August, 7}", {"September, 8}", {"October, 9}",
{"November, 10}", {"December, 11}" };
cout << "Enter the name of the month: " << endl;
cin >> m;
while (m.find(m) == m.end())
{
cout << "Invalid month entered! Exit the program to try again."
<< endl;
cout << "Enter the name of the month: " << endl;
cin >> m;
}
cout << endl;
cout << "There are " << days[i[m]] << " days in "
<< m << "." << endl;
return 0;
}
我想
这是一个作业?对于主函数,您可以使用地图。以下是一些提示,以便您可以完成任务。另请记住,字符串比较区分大小写,需要解决。
#include <stdio.h>
#include <string>
#include <map>
struct comparer
{
public:
bool operator()(const std::string x, const std::string y) const
{
return x.compare(y)==0;
}
};
int main()
{
map<string, int> months;
months.insert(pair<string, int>("january", 31));
months.insert(pair<string, int>("february", 28));
months.insert(pair<string, int>("mars", 31));
months.insert(pair<string, int>("april", 30));
cout << "Enter the name of the month: " << endl;
cin >> m;
std::map<std::string, int, comparer>::iterator it=months.find(m);
if(it!=months.end())
printf("The number of days is %dn",(*it).second);
else
printf("Error, the month not foundn");
}
相关文章:
- 正在尝试了解输入验证循环
- 如何在C++中检查2D数组中负值的输入验证
- cin 的十进制输入验证?
- 输入验证以筛选出字符、字符串和一系列整数
- 输入验证将数字放入向量中
- 在C++中使用字符数组进行输入验证
- 在C++中使用字符数组进行用户输入验证
- C++ 中字符数组的输入验证
- 检查节点的输入验证的整数
- 如何让我的程序在输入验证的基础上读取素数
- C++ 用户输入验证无法正常工作
- 程序终止,而不是继续整数输入验证循环
- 使用REGEX进行电子邮件输入验证C
- C++:输入验证器函数
- 将用户输入验证为整数:用户必须两次输入输入
- C 输入验证
- C :对输入验证函数进行编程的性能影响可以对其进行编程,而每次都可以将其编程
- 使用模板函数进行通用输入验证
- 为什么用于输入验证的 while 循环要么完全跳过循环,要么不考虑错误的输入
- 仅 C++ 字母和空格输入验证