我如何使这个循环工作?
问题描述:
我应该收集数字的输入,直到用户键入'0'。当用户决定退出循环时,用户可以决定是否重新开始。 如何修复我的程序?还是有另一种更有效的方式来提供这个程序?我如何使这个循环工作?
`
int regular, special, vip;
int i;
bool flag = true;
while(flag)
{
cout << "input please: ";
cin >> i;
if ((i <10000) && (i > 0))
{
regular++;
}
if ((i >10000) && (i<=50000))
{
special++;
}
if (i > 50000)
{
vip++;
}
if (i==0);
{
flag = false;
cout << "Your branch has " << regular << " Regular Customers, "
<< special << " Special Customers and " << vip << " VIP Customers"
<< "Try again? [1 = yes/ 0 = no] " << endl;
}
}
}`
答
这应该工作。 cout
之前设置flag
为false。
同时删除分号后if(i==0)
int regular, special, vip;
int i;
bool flag = true;
while(flag)
{
cout << "input please: ";
cin >> i;
if ((i <10000) && (i > 0))
{
regular++;
}
if ((i >10000) && (i<=50000))
{
special++;
}
if (i > 50000)
{
vip++;
}
if (i==0)
{
cout << "Your branch has " << regular << " Regular Customers, "
<< special << " Special Customers and " << vip << " VIP Customers"
<< "Try again? [1 = yes/ 0 = no] " << endl;
flag = false;
}
}
答
如果我理解你的要求正确,你就必须在用户的选择读再试一次,做基础的东西。哦,并删除分号后if(i==0)
:
if (i==0)
{
flag = false;
cout << "Your branch has " << regular << " Regular Customers, "
<< special << " Special Customers and " << vip << " VIP Customers"
<< "Try again? [1 = yes/ 0 = no] " << endl;
/* Do what the user wants */
cin>> i;
if(i == 1)
{
/* Reset regular, special, and vip variables if desired */
flag = true;
}
}
一个良好的开端。将除去从'分号如果(I == 0);' – paddy
@paddy OnAncientJoke:'如果(incoming_detected() ); launch_nuclear_counterstrike();' –
jfc我盲谢谢! – asteron9