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

时间:2018-04-01 03:32:58

标签: c++ alphanumeric

此程序的目的是检查用户输入的字符是否为字母数字。一旦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 "";
    }    

2 个答案:

答案 0 :(得分: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测试。仍然有效)

答案 1 :(得分:0)

当我在我的设置(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语句是最重要的任务。