2015-09-06 69 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