简体   繁体   中英

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++.

When you type 'a', there is nothing to eat the input. This will keep failing, going "insane"

cin >> a;

Reads numbers into 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).

You can do that with cin.clear() and 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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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