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

时间:2015-09-06 13:38:22

标签: c++

我不知道发生了什么,但每次我把信件和特殊字符疯狂地循环,我不知道如何纠正它。我认为这是克劳德的一部分。

#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;   
    }

代码正常运行且工作正常。请在您自己的开发C ++中查找结果。

3 个答案:

答案 0 :(得分:1)

当您输入'a'时,没有任何东西可以输入。这将继续失败,变得“疯狂”

cin >> a;

将数字读入。如果它无法读取,则会设置错误并且不会读取任何内容。

答案 1 :(得分:1)

您必须清除控制台输入缓冲区(std :: cin)。

你可以用cin.clear()和cin.ignore()

来做到这一点
//...
cout<<endl;
cout<<"Press Any Key To Continue "<<endl;

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

答案 2 :(得分:0)

当您重新进入 Josh goto 语句并且cin&gt;&gt; b由于 输入流 中的先前值而未接受输入时,会发生这种情况> 因此,只需通过调用 cin.ignore();

清除输入流
#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;   
    }