【问题标题】:Problem with boolean variable in email validation program [duplicate]电子邮件验证程序中的布尔变量问题[重复]
【发布时间】:2019-05-03 16:22:29
【问题描述】:

我正在尝试在不使用正则表达式库的情况下创建电子邮件验证程序。在我的一个函数中,我想返回一个布尔值来检查电子邮件地址中是否有 @ 符号以及它是否处于有效位置(@ 符号不能是字符串的前三个字符之一)。但是我遇到了问题,因为每次我通过输入带有@符号无效位置的电子邮件地址来运行程序时,它一直告诉我该电子邮件是有效的。请帮忙!

valid = checkEmail(email); //function call 

if(valid == true)
  cout << "Your email is valid!" << endl;

else
  cout << "Your email is invalid!" << endl;


bool checkEmail(string email)
{
  int counter;
  int length;
  bool firstThree; //checks to make sure @ is not in the first three chars

  counter = 0;
  length = email.length();
  firstThree = false;

  for(int i = 0; i < length; i++)
  {
    if(email[i] == '@')
      counter++;
  }

 for(int y = 0; y < length; y++)
 {
   if(email[0] == '@' || email[1] == '@' || email[2] == '@')
      firstThree = true;

   else
       firstThree = false;

 }

 cout << "\n" << counter << endl; //check to see if counter works

 if(counter != 1 && firstThree == true)
   return false;

 else
   return true;
}

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programsDebugging Guide
  • if(counter != 1 &amp;&amp; firstThree == true) 仅当您的电子邮件没有 @ ind 时才会返回 false。AND 如果 @ 位于前三个字符中。另请注意,围绕 firstThree 检查的 for 循环没有任何用处。
  • 还可以查看std:.string::find_first_of,它返回字符串的第一次出现。有了它,您的任务将非常简单。当你想做类似的事情时,也可以看看std::basic_string,因为它提供了许多有用的实用程序

标签: c++ c++11


【解决方案1】:

我猜你需要在程序开始时定义函数bool checkEmail(string email)。基本上翻转 if else 和函数定义。

bool checkEmail(string email)
{
  int counter;
  int length;
  bool firstThree; //checks to make sure @ is not in the first three chars

  counter = 0;
  length = email.length();
  firstThree = false;

  for(int i = 0; i < length; i++)
  {
    if(email[i] == '@')
      counter++;
  }

 for(int y = 0; y < length; y++)
 {
   if(email[0] == '@' || email[1] == '@' || email[2] == '@')
      firstThree = true;

   else
       firstThree = false;

 }

valid = checkEmail(email); //function call 

if(valid == true)
  cout << "Your email is valid!" << endl;

else
  cout << "Your email is invalid!" << endl;

【讨论】:

    【解决方案2】:

    这里似乎有很多初学者的错误,所以我建议先学习编程和 c++ 的基础知识,就像其他人建议的那样,使用调试器。以下是一些错误:

    函数定义

    c++ 与其他通用语言不同,因为函数需要先定义才能使用。这意味着您要么需要将 checkEmail 函数移到函数调用之上,要么在函数调用之上创建一个单独的定义,例如:

    bool checkemail(string email);
    

    作为一个例子。

    if 语句逻辑不正确;不必要的 for 循环:

    我假设根据电子邮件的格式,如果 checkEmail 函数与正确的格式不匹配,则您希望它返回 false,并且根据您的函数当前所做的,这意味着它将返回如果前三个字符是 @ 如果电子邮件中不完全有一个 @ 符号,则返回 false。但是,您使用了&amp;&amp; 运算符,它表示and,这意味着即使您不希望它返回true(就像@Yastanub 在他的第一条评论中所说的那样)。更好的是,使用std::string::findwhile 循环和向量(类似于this method)可以大大简化整个逻辑和if 语句:

    vector<size_t> posVec;
    size_t pos = email.find('@', 0); //note that this can be an int instead of size_t, but it can cause stack overflow with bigger numbers
    while(pos != string::npos){
        posVec.push_back(pos);
        pos = email.find('@', pos+1);
    }
    switch(posVec.size()){
        //if there is only one @ symbol found
        case 1:
            bool firstThree = false;
            for(int i = 0; i <= 2; i++)
                //if there is only one @ but it's in the first 3 positions, the email isn't valid
                if(posVec[0] == i)
                    firstThree = true;
            return !firstThree;
        //otherwise the email doesn't work
        default:
            return false;
    }
    

    请记住包含所需的库以使这部分工作:

    #include <string>
    #include <vector>
    

    这也消除了函数中第二个无用的 for 循环,因为变量 y 没有被使用,而 counter 正被用于测试

    另一个说明

    也没有必要使用if (valid == true)。您可以只使用 if (valid),因为布尔值如何与 if 语句一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 2011-07-16
      • 2018-12-22
      • 2019-06-16
      • 1970-01-01
      • 2012-10-03
      • 2011-06-21
      相关资源
      最近更新 更多