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

标签 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 "";
    }    

最佳答案

我通过更改 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 进行了测试。仍然有效)

关于c++ - 检查输入是否为字母数字时出现逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594545/

相关文章:

android - JNI_VERSION_1_6 与 JNI_VERSION_1_4

c++ - 指针和递归性——我正在努力节省内存

c++ - 共享内存大小计算c++

c++ - 使用 OpenCV 找到没有狭窄瓶颈的连续区域

sql - 对包含字母数字条目的 VARCHAR 列进行排序

检查用户是否在 C 中输入字母或数字

java - 在 C++ 中是否有 Java 'volatile' 的等价物?

javascript - 需要一个正则表达式来验证长度为 6 到 25 个字符的字母数字

javascript - 文本输入只允许英文字符和数字

php - 递增 alpha 数值