如何验证用户将字符串输入到std :: cin中

How can I verify user inputted a string into std::cin?

本文关键字:输入 std 字符串 cin 何验证 验证 用户      更新时间:2023-10-16

students[#]是结构的数组, students[#].name是struct中的字符串,我想确保当用户输入该名称被确认为字符串时。<<<<<<<<<<<<<<<<<<<</p>

有人可以告诉我(简单?(完成这项工作的方法吗?

我尝试了以下内容。

        bool checkString(const char *str)
        {
            bool isLetters = true;
            while (*str != '')
            {
                if (*str < '65' || *str > '122')
                {
                   //checking to see if it's over or below ASCII alpha characters
                    isLetters = false;
                }
                if (*str < '97' && *str > '90')
                {
                     // checking to see if it's between capital and 
                     //lowercase alpha char's
                    isLetters = false;
                }
                ++str;
            }
            return isLetters;
        }
        main
        {
        //...
                for (int i = 0; i < numStudents; ++i)
                {
                    valid = true;
                    do
                    {
                        valid = true;
                        cout << "Enter NAME for student #" << i + 1 << ":" << endl;
                        cin >> students[i].name;
                        if (cin.fail() || !checkString(students[i].name.c_str()))
                            // tried to see if i could convert it to a c-style 
                            //string to check char by char
                        {
                            cin.clear();
                            cin.ignore();
                            cout << "Please enter a valid input." << endl;
                            valid = false;
                        }
                    } while (!valid);
        //...
        return 0;
        }
        `

ascii代码是整数,不要将它们放在引号中。

bool checkString(const char *str)
{
    bool isLetters = true;
    while (*str != '')
    {
        if (*str < 65 || *str > 122)
        {
           //checking to see if it's over or below ASCII alpha characters
            isLetters = false;
        }
        if (*str < 97 && *str > 90)
        {
             // checking to see if it's between capital and 
             //lowercase alpha char's
            isLetters = false;
        }
        ++str;
    }
    return isLetters;
}

您仍在做艰难的方法,该功能isalpha是为此任务设计的

#include <cctype>
bool checkString(const char *str)
{
    bool isLetters = true;
    while (*str != '')
    {
        if (!isalpha(*str)))
            isLetters = false;
        ++str;
    }
    return isLetters;
}

然后,当您意识到这不是字母时,您可以立即返回,进行检查

没有意义
#include <cctype>
bool checkString(const char *str)
{
    while (*str != '')
    {
        if (!isalpha(*str)))
            return false;
        ++str;
    }
    return true;
}

您必须使用ASCII值检查每个char是否是字母。

bool checkString(string stdunt)
    {
        bool isLetters = true;
        for (int i = 0; i < stdunt.length(); i++)
            {
                    if (stdunt[i] < 65 || stdunt[i] > 122)
                    { return false; }
                    if (stdunt[i] < 97 && stdunt[i] > 90)
                    { return false; }
            }
        return isLetters;
    }
int main()
{
int numStudents=5;
string students[numStudents];
    for (int i = 0; i < numStudents; i++)
    {
        bool valid = true;

    do{
            valid = true;
            cout << "Enter NAME for student #" << i + 1 << ":" << endl;
            cin >> students[i];
//            cout <<students[i];
            if (cin.fail() || !checkString(students[i]))
            {
                cin.clear();
                cin.ignore();
                cout << "Please enter a valid input." << endl;
                valid = false;
            }
      } while (!valid);
    }
    cout << "Complete";
    return 0;
}
`