检查输入是否为字母数字时的逻辑错误

Logic error when checking if input is alphanumeric

本文关键字:错误 数字 输入 是否 检查      更新时间:2023-10-16

该程序的目的是检查用户输入的字符是否为字母数字。一旦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(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进行了测试。仍然有效(

在我的设置(G 6.4,cygwin(中尝试时,我没有得到任何输出。当我向输出线添加<< 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语句是最重要的任务。