[英]Loop if i input letters or special characters
我不知道发生了什么,但是每次我输入字母和特殊字符时它都会疯狂循环,我不知道如何纠正它。 这是我从克劳德的部分认为的。
#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++ 中查找结果。
当你输入 'a' 时,没有什么可以吃掉输入的。 这将继续失败,变得“疯狂”
cin >> a;
将数字读入 a。 如果无法读取,则设置错误并且不读取任何内容。
您必须清除控制台输入缓冲区 (std::cin)。
你可以用 cin.clear() 和 cin.ignore() 做到这一点
//...
cout<<endl;
cout<<"Press Any Key To Continue "<<endl;
cin.clear( );
cin.ignore( 10000, '\n' );
//...
当它发生在你重新进入乔希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.