简体   繁体   English

如果我输入字母或特殊字符,则循环

[英]Loop if i input letters or special characters

I don't know what happened but everytime I put Letters and Special Characters It loops insanely, and I dont know how to correct it.我不知道发生了什么,但是每次我输入字母和特殊字符时它都会疯狂循环,我不知道如何纠正它。 It is i think from the claude part.这是我从克劳德的部分认为的。

#include<iostream>
using namespace std;
    int main()
    {
        int a,b;
    Claude:
    {
    cout<<"Please Enter Your Pin: ";
    cin>>a;
    cout<<endl;
    system("cls");
    cin.get();
    }

        if(a==1234)
    {       Josh:
           cout<<"Choose An Option: "<<endl;
           cout<<"1: Withdraw                 2: Check Balance";
           cout<<endl;
           cout<<"3: Deposit                  4: Exit";
           cout<<endl;
          cout<<"Enter Here: ";
          cin>>b;
          system("cls");
    }
    else
        {
        cout<<"Incorrect Pin, Please Try Again"<<endl;
        cout<<endl;
        cout<<"Press Any Key To Continue "<<endl;
        system("pause>nul");
        system("cls");
        goto Claude;


    }

      if(b==1)
        {
        cout<<"How Much: ";

        }
    else if (b==2)
        {
        cout<<"2000.00";

        }
    else if(b==3)
        {
        cout<<"Nothing To Withdraw";

        }
    else if(b==4)
        {
        cout<<"test";
        }   
    else
        {
            cout<<"Please Enter Valid Input, Press Any Key To Continue ";
            cin.get();
            system("cls");
            goto Josh;  
        }


    return 0;   
    }

The code is working without error and working properly.代码工作正常且没有错误并且工作正常。 Please look for the result in your own dev C++.请在您自己的开发 C++ 中查找结果。

When you type 'a', there is nothing to eat the input.当你输入 'a' 时,没有什么可以吃掉输入的。 This will keep failing, going "insane"这将继续失败,变得“疯狂”

cin >> a;

Reads numbers into a.将数字读入 a。 If it is unable to read, then it sets an error and reads nothing.如果无法读取,则设置错误并且不读取任何内容。

You have to clear your console input buffer (std::cin).您必须清除控制台输入缓冲区 (std::cin)。

You can do that with cin.clear() and cin.ignore()你可以用 cin.clear() 和 cin.ignore() 做到这一点

//...
cout<<endl;
cout<<"Press Any Key To Continue "<<endl;

cin.clear( );
cin.ignore( 10000, '\n' );
//...

it happens when your reentered in to Josh goto statement and cin>>b doesn't take input because of previous value in inputstream So, just clear the inputstream by calling cin.ignore();当它发生在你重新进入乔希goto语句和CIN >> B不接受,因为在以前的InputStream值的输入所以,仅仅通过调用cin.ignore(清除的InputStream);

#include<iostream>
#include<limits>
    using namespace std;
    int main()
    {
        int a,b;
    Claude:
    {
    cout<<"Please Enter Your Pin: ";
    cin>>a;
    cout<<endl;
    system("cls");
    cin.get();
    }

        if(a==1234)
        {
         Josh:

           cout<<"Choose An Option: "<<endl;
           cout<<"1: Withdraw                 2: Check Balance";
           cout<<endl;
           cout<<"3: Deposit                  4: Exit";
           cout<<endl;
           cout<<"Enter Here: ";
           //cin.ignore(numeric_limits<streamsize>::max(),'*');
           cin>>b;

          system("cls");
    }
    else
        {
        cout<<"Incorrect Pin, Please Try Again"<<endl;
        cout<<endl;
        cout<<"Press Any Key To Continue "<<endl;
        system("pause>nul");
        system("cls");
        goto Claude;


    }

      if(b==1)
        {
        cout<<"How Much: ";

        }
    else if (b==2)
        {
        cout<<"2000.00";

        }
    else if(b==3)
        {
        cout<<"Nothing To Withdraw";

        }
    else if(b==4)
        {
        cout<<"test";
        }   
    else
        {
            cout<<"Please Enter Valid Input, Press Any Key To Continue ";





        cin.clear();
        cin.ignore();



        cin.get();
        cin.get();

        system("cls");
        goto Josh;  
        }


    return 0;   
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM