Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ 如果在输入中为数字输入一个字符,程序将进入无限循环_Visual C++ - Fatal编程技术网

Visual c++ 如果在输入中为数字输入一个字符,程序将进入无限循环

Visual c++ 如果在输入中为数字输入一个字符,程序将进入无限循环,visual-c++,Visual C++,我似乎无法让错误检查在GetInput阶段正常工作。我试着让那个阶段重复,直到数字1-5被放进去,没有别的。谢谢大家! void GetInput(void) { cout << "Please enter a number between 1 and 5: \n"; cin >> OnetoFive; if (isdigit(OnetoFive) && (OnetoFive <= 5) && (OnetoFi

我似乎无法让错误检查在GetInput阶段正常工作。我试着让那个阶段重复,直到数字1-5被放进去,没有别的。谢谢大家!

void GetInput(void)
{
    cout << "Please enter a number between 1 and 5: \n";
    cin >> OnetoFive;
    if (isdigit(OnetoFive) && (OnetoFive <= 5) && (OnetoFive >= 1))
    {
        return;
    }

    else 
    {
        system("cls");
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "\n\nInvalid.  Please enter a number between 1 and 5: ";
        cin >> OnetoFive;
    }
}
void GetInput(void)
{
不能超过一个生命;
如果(isdigit(OnetoFive)&(OnetoFive=1))
{
返回;
}
其他的
{
系统(“cls”);
cin.clear();
cin.ignore(INT_MAX,'\n');
不能超过一个生命;
}
}

好的,现在它对字母字符有效,但如果我做的值超过5,循环仍然不起作用…它工作一次,但第二次它只是运行…有什么想法吗首先,为什么要请求输入两次?你的循环应该注意这个

第二,如果条件允许,请更改您

   if (( isdigit(OnetoFive) && OnetoFive >= 1) && (OnetoFive <= 5))

如果((isdigit(OnetoFive)和&OnetoFive>=1)和(OnetoFive您是否尝试更换:

else 
{
    cin.clear();
    cout << "\n\nInvalid.  Please enter a number between 1 and 5: ";
    cin >> OnetoFive;
    if ((OnetoFive >= 1) && (OnetoFive <= 5) && isdigit(OnetoFive))
    {
        system("cls");
        return;
    }

}

C++不是我的强项,但为了让您了解适合您需求的循环是什么样子:

bool GetInput()
{
    cout << "Please enter a number between 1 and 5: \n";
    cin >> OnetoFive;

    if(isdigit(OnetoFive) && (OnetoFive <= 5) && (OnetoFive >= 1))
        {
            return true;
        }
    else 
        {
        system("cls");
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "\n\nInvalid.  Please enter a number between 1 and 5: ";
        }   
    return false;
}

while(!GetInput())
{
    GetInput();
}
bool GetInput()
{
不能超过一个生命;
如果(isdigit(OnetoFive)&(OnetoFive=1))
{
返回true;
}
其他的
{
系统(“cls”);
cin.clear();
cin.ignore(INT_MAX,'\n');

天哪,你们太快了!我要编辑这个坏小子,然后再试一次,谢谢!好的,对字母字符进行了条件检查…现在它的数字超过5是一个令人头痛的问题,有什么想法吗?只是第二次不起作用,对吧?第二次你问你时,你不会验证。你应该-真的-做一个验证和inpu的循环T
bool GetInput()
{
    cout << "Please enter a number between 1 and 5: \n";
    cin >> OnetoFive;

    if(isdigit(OnetoFive) && (OnetoFive <= 5) && (OnetoFive >= 1))
        {
            return true;
        }
    else 
        {
        system("cls");
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "\n\nInvalid.  Please enter a number between 1 and 5: ";
        }   
    return false;
}

while(!GetInput())
{
    GetInput();
}