如何使用数组字符串进行输入验证

How to do a input validation with a string of array?

本文关键字:输入 验证 字符串 何使用 数组      更新时间:2023-10-16

我正在编写一个程序,我必须输入一个月,然后输出该月有多少天。但是,我还必须编写一个代码,如果输入不是有效的月份,它会显示一条错误消息。我无法弄清楚如何输出该错误语句。不过我已经弄清楚了其余的。

我已经尝试了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");
}