【问题标题】:Logic error when checking if input is alphanumeric检查输入是否为字母数字时出现逻辑错误
【发布时间】:2018-04-01 03:32:58
【问题描述】:

这个程序的目的是检查用户输入的字符是否是字母数字。一旦 void 函数确认输入正确,然后将其传递给字符串测试以输出消息。我知道这不是很好的编码,但必须以这种方式完成。

我不断收到逻辑错误,我不知道为什么?有人可以帮忙吗?

#include <iostream> 
#include <string>
#include <cctype>

using namespace std;

void get_option(char& input);
/**
Takes character entered user input and loops until correct answer
@param y character entered by user
@return to main() once valid entry received
*/
string test(char);
/**
Takes checks character entered user input and loops until correct answer
@param y alphanumeric character entered by user
@return to main() once valid entry received
*/

int main()
{
    char y;

    //call get_option to prompt for input
    get_option(y);
    //call test after user input is valid
    test(y);

    return 0;
}

void get_option(char &x)
    {

        cout << "Please enter an alphanumeric character: ";
        cin >> x;

        while (!(isdigit(x)||islower(x)||isupper(x)))
        {
            cout << "Please enter an alphanumeric character: ";
            cin >> x;
        }
    }    

string test(char y)
    {
        if (isupper(y))
        {
            cout << "An upper case letter is entered!";
            } else if (islower(y)) { 
                cout << "A lower case letter is entered!";
                } else if (isdigit(y)) {
                cout << "A digit is entered!";
        }
        return "";
    }    

【问题讨论】:

  • 为什么将test声明为返回一个string,然后从中返回0(不是字符串),然后完全忽略它的返回值?
  • 有一个东西叫isalpha()。此外,如果您使用char 作为输入,像isalpha() 这样的函数在技术上具有未定义的行为,因为它需要unsigned chars。您应该先将其转换为无符号字符。 en.cppreference.com/w/cpp/string/byte/isdigit
  • 更好的是,std::isalnum() 检查字母数字字符。

标签: c++ alphanumeric


【解决方案1】:

我通过更改test(char) 函数的返回类型让程序完美运行:

#include <iostream> 
#include <string>
#include <cctype>

using namespace std;

void get_option(char& input);
/**
Takes character entered user input and loops until correct answer
@param y character entered by user
@return to main() once valid entry received
*/
int test(char); //Changed from string to int
/**
Takes checks character entered user input and loops until correct answer
@param y alphanumeric character entered by user
@return to main() once valid entry received
*/

int main()
{
    char y;

    //call get_option to prompt for input
    get_option(y);
    //call test after user input is valid
    test(y);

    return 0;
}

void get_option(char &x)
    {

        cout << "Please enter an alphanumeric character: ";
        cin >> x;

        while (!(isdigit(x)||islower(x)||isupper(x)))
        {
            cout << "Please enter an alphanumeric character: ";
            cin >> x;
        }
    }    

int test(char y) //Also changed from string to int
    {
        if (isupper(y))
        {
            cout << "An upper case letter is entered!";
            } else if (islower(y)) { 
                cout << "A lower case letter is entered!";
                } else if (isdigit(y)) {
                cout << "A digit is entered!";
        }
        return 0;
    }   

(使用 C++14 编译器在 JDoodle 上进行测试。) (另外,使用 Xcode 测试。仍然有效)

【讨论】:

    【解决方案2】:

    当我在我的设置(g++ 6.4,cygwin)中尝试它时,我没有得到任何输出。当我将&lt;&lt; endl 添加到输出行时,输出出现了。

    我怀疑你遇到了同样的问题。

    string test(char y)
    {
       if (isupper(y))
       {
          cout << "An upper case letter is entered!" << endl;  // Add endl
       }
       else if (islower(y))
       { 
          cout << "A lower case letter is entered!" << endl;
       }
       else if (isdigit(y))
       {
          cout << "A digit is entered!" << endl;
       }
    
       // This does not make sense but it is syntactically valid.
       return 0;
    }
    

    JiveDadson 是对的。问题出在return 0 行。它会导致未定义的行为。将该行更改为

    return "";
    

    修复了输出问题,endl 与否。拥有endl 更好,但不是必需的。修复return 语句是最重要的任务。

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2013-08-05
      • 1970-01-01
      • 2013-08-07
      • 2020-07-28
      相关资源
      最近更新 更多