如何限制以下代码的输入数量? C ++

时间:2015-05-01 05:45:02

标签: c++ c++11 conditional-statements

所以基本上这个代码只不过是用户购买枪支的三个商店。用户必须按退出以退出一个商店并进入其他商店。

我想对用户施加限制,他可以从这三个部分集体购买10支枪。

例如,如果用户从第一家商店购买10支枪,那么应该跳过从其他商店购买的过程。假设用户从第一家商店购买6支枪,从另一支商店购买4支枪,则应跳过第三家商店。

有任何想法如何实现这个?

cout<<endl<<"enter the guns you want from store 1 and press exit to buy from other sstore"<<endl;
cin>>a;

while(a!="exit")
{
    tmp = false;
    for(map<string,int> :: const_iterator it = storeone.begin(); it != storeone.end(); ++it)
    {  
        if(it->first == a)
        {
            b=it->second;
            setfinalguns(a,b);
            cout<<"you bought "<<a<<" for price "<<b<<endl;
            spentmoney(b);
            tmp = true;
            break;
        }
    }
    if(!tmp)
        cout<<"This gun is not available"<<endl;
    cin>>a;

} 

cout<<"enter the items you want to  from store two and press exit to buy from other store"<<endl;
cin>>c;
while(c!="exit")
{
    tmp = false;
    for(map<string,int> :: const_iterator it = stroretwo.begin(); it != storetwo.end(); ++it)
    {
        if(it->first == c)
        {
            d=it->second;
            setfinalguns(c,d);
            cout<<"you bought "<<c<<" for price "<<d<<endl;
            spentmoney(d);
            tmp = true;
            break;
        }
    }
    if(!tmp)
        cout<<"Thisgun is not available"<<endl;
    cin>>c;

} 

cout<<"enter the guns you want from store three and press exit to buy from other store"<<endl;
cin>>e;
while(e!="exit")
{
    tmp = false;
    for(map<string,int> :: const_iterator it = storethree.begin(); it != storethree.end(); ++it)
    {
        if(it->first == e)
        {
            f=it->second;
            setfinalguns(e,f);
            cout<<"you bought "<<e<<" for price "<<f<<endl;
            spentmoney(f);
            tmp = true;
            break;
        }
    }
    if (!tmp)
        cout<<"This gun is not available"<<endl;
    cin>>e;
    break;
}

2 个答案:

答案 0 :(得分:0)

以下是修改后的代码,可以解决您的问题。你只需要添加一个计数器变量(全局),它将跟踪购买的枪支数量,并在每次用户购买枪支时增加它。

现在,在while循环的每次迭代中,检查计数器的值,如果不是则中断。买的枪是10。

int counter=0;   //This will keep track of number of guns bought
cout<<endl<<"enter the guns you want from store 1 and press exit to buy from other sstore"<<endl;
cin>>a;

while(a!="exit")
{
  if(counter == 10)  // check if user has already bought 10 guns
    break;
  tmp = false;
  for(map<string,int> :: const_iterator it = storeone.begin(); it != storeone.end(); ++it)
  {  
    if(it->first == a)
    {
      b=it->second;
      setfinalguns(a,b);
      cout<<"you bought "<<a<<" for price "<<b<<endl;
      spentmoney(b);
      counter++;
      tmp = true;
      break;
    }
  }
  if(!tmp)
    cout<<"This gun is not available"<<endl;
  cin>>a;

} 

cout<<"enter the items you want to  from store two and press exit to buy from other store"<<endl;
cin>>c;
while(c!="exit")
{
  if(counter == 10)
    break;
  tmp = false;
  for(map<string,int> :: const_iterator it = stroretwo.begin(); it != storetwo.end(); ++it)
  {
    if(it->first == c)
    {
      d=it->second;
      setfinalguns(c,d);
      cout<<"you bought "<<c<<" for price "<<d<<endl;
      spentmoney(d);
      counter++;
      tmp = true;
      break;
    }
  }
  if(!tmp)
    cout<<"Thisgun is not available"<<endl;
  cin>>c;

} 

cout<<"enter the guns you want from store three and press exit to buy from other store"<<endl;
cin>>e;
while(e!="exit")
{
  if(counter == 10)   
    break;
  tmp = false;
  for(map<string,int> :: const_iterator it = storethree.begin(); it != storethree.end(); ++it)
  {
    if(it->first == e)
    {
      f=it->second;
      setfinalguns(e,f);
      cout<<"you bought "<<e<<" for price "<<f<<endl;
      spentmoney(f);
      counter++;
      tmp = true;
      break;
    }
  }
  if (!tmp)
    cout<<"This gun is not available"<<endl;
  cin>>e;
  break;
}

答案 1 :(得分:0)

作为我之前评论的解释,您可以保留一个变量来存储购买的枪支数量。

然后,添加一个简单的if语句,如

if( number_of_guns == 10 )
   break;

在你的while循环开始时。所以更简单一点,如果购买的枪支数量为10,则从你的while循环中突破。

另外,要将枪支数量限制为10,您可以尝试类似

的操作
if( number_of_guns > 10 )
      Try_again ;  // or continue

这是关于如何操作的信息,你可以尝试实现它。