2015-09-06 38 views
-5

我不知道发生了什么,但每次我把字母和特殊字符疯狂地循环,我不知道如何纠正它。这是我从克劳德角度想到的。循环如果我输入字母或特殊字符

#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++中查找结果。

+0

删除不需要的,使你的代码更不可读 –

+1

建议额外的括号:不使用goto语句(http://stackoverflow.com/questions/3517726/what-is-wrong-with-使用goto) – CIsForCookies

+0

你能否请建议其他代码转到我不知道替代品 –

回答

1

当你输入'a'时,没有东西可以吃掉输入。这将继续失败,走向“疯狂”

cin >> a; 

将数字读入a。如果它无法读取,那么它会设置一个错误并且什么也不读。

+0

没有得到它,所有我知道的是,当我输入字母它的故障 –

1

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

你可以做到这一点与cin.clear()和cin.ignore()

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

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

我写的那部分,但它不清除前面的输出 –

0

当你在重新输入到乔希转到声明和CIN >> B并不需要输入,因为它发生之前的值为inputstream 所以,只需通过调用来清除输入流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; 
    } 
+0

它仍然循环时,我输入字母 –

+0

add cin.get();最后 – rewrihitesh

相关问题