【问题标题】:how to limit the number of inputs for the following code? c++如何限制以下代码的输入数量? C++
【发布时间】:2015-07-11 00:11:56
【问题描述】:

所以基本上下面的这段代码只不过是用户购买枪支的三个商店。用户必须按退出才能离开一家商店并进入另一家商店。

我想限制用户总共只能从所有三个部分购买 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;
}

【问题讨论】:

  • if( number_of_guns == 10 ) break;这样的东西怎么样

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


【解决方案1】:

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

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

if( number_of_guns == 10 )
   break;

在您的 while 循环开始时。简单来说,如果购买的枪支数量为 10,则从您的 while 循环中跳出。

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

if( number_of_guns > 10 )
      Try_again ;  // or continue

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

【讨论】:

    【解决方案2】:

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

    现在,在 while 循环的每次迭代中,检查 counter 的值,如果不是则中断。购买的枪支数量为 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2010-12-14
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多