【问题标题】:FOR loop without condition value "for (int i = 1; ; i++)" doesn't work properly没有条件值的 FOR 循环 "for (int i = 1; ; i++)" 不能正常工作
【发布时间】:2016-01-21 04:09:26
【问题描述】:

我发现了一个帖子,其中使用了 FOR 循环而没有 CONDITION 值。这是一个循环:

for (INITIALIZATION; CONDITION; AFTERTHOUGHT) 
{
    // Code for the for-loop's body goes here.
}

跳过 CONDITION 值是不安全的,但如果你使用if/else 语句,它可以做到。请看一下我的 for 循环:for (int i = 1; ; i++) 和里面的实现。出于某种原因,if/else 语句我没有得到正确的逻辑。

#include <iostream>
using namespace std;

int main() {

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else
            boxes -= boxes_for_sale;
            cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;

        if (boxes == 0)
            cout << "Sold out!" << endl;
            break;

    }
    return 0;
}

【问题讨论】:

  • 您在几个地方缺少一些大括号 { }
  • 正如@1201ProgramAlarm 所说,您需要在if 和else 语句之后将代码括起来。此外,“它不能正常工作”也不是一个好的问题描述。您应该添加有关如何它不起作用以及您预期会发生什么的信息。

标签: c++ if-statement for-loop


【解决方案1】:

由于您没有指定程序所需的行为,我将仅说明代码中发生的情况。

可能会做一些意想不到的代码的重要sn-ps:

 for (int i = 1;; i++) 

这是完全合法的,但请注意没有指定终止条件。除非您 break 退出,否则您将陷入无限循环。

else
    boxes -= boxes_for_sale;
    cout << "Car N:" << i << " is full\n" <<
    "You have " << boxes << "boxes for sale" << endl;

我可以根据缩进猜测,您希望两行仅在 else 之前的 if 语句为假时执行,但这不是正在发生的事情。

由于您没有使用花括号指定 else 块,因此只有 else 之后的第一条语句会在您需要时执行。 cout始终执行。

你有同样的问题:

if (boxes == 0)
        cout << "Sold out!" << endl; //will output only if boxes==0
        break; //will break out of loop in any case

【讨论】:

    【解决方案2】:
    #include <iostream>
    using namespace std;
    
    int main() {
    
        int boxes;
        int boxes_for_sale;
    
        cout << "Enter quantity of the boxes in warehouse: > " << flush;
        cin >> boxes;
        cout << "Enter quantity of the boxes for sale: > " << flush;
        cin >> boxes_for_sale;
    
        for (int i = 1;; i++) {
    
            if (boxes < boxes_for_sale) {
                cout << "There are not enough boxes in warehouse!" << endl;
                cout << "Enter quantity of the boxes for sale: > " << flush;
                cin >> boxes_for_sale;
            }
            else{
                boxes -= boxes_for_sale;
    }
                cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;
    
            if (boxes == 0)
     {           cout << "Sold out!" << endl;
                break;}
    
        }
        return 0;
    }
    

    【讨论】:

    • 解释你做了什么以及为什么强烈推荐。
    【解决方案3】:

    注意大括号,你需要为每个for循环,if语句和else语句放置大括号。

    还要注意缩进,以实现良好的编程习惯。

    #include <iostream>
    using namespace std;
    
    int main() 
    {
    
        int boxes;
        int boxes_for_sale;
    
        cout << "Enter quantity of the boxes in warehouse: > " << flush;
        cin >> boxes;
        cout << "Enter quantity of the boxes for sale: > " << flush;
        cin >> boxes_for_sale;
    
        for (int i = 1;; i++) {
    
            if (boxes < boxes_for_sale) 
            {
                cout << "There are not enough boxes in warehouse!" << endl;
                cout << "Enter quantity of the boxes for sale: > " << flush;
                cin >> boxes_for_sale;
            }
            else
            {
                boxes -= boxes_for_sale;
                cout << "Car N:" << i << " is full\n" << "You have " << boxes <<       boxes for sale" << endl;
            }
            if (boxes == 0)
           {
                cout << "Sold out!" << endl;
                break;
           }
    
        }
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      对不起,伙计们缺乏描述。这是我在这里的第一个问题,下次我会更具体。

      这是我尝试做的。

      int main() {
      
          int boxes_in_warehouse;
          int boxes_for_sale;
          cout << "Enter quantity of the boxes in warehouse: " << flush;
          cin >> boxes_in_warehouse;
          cout << "Enter quantity of the boxes for sale: " << flush;
          cin >> boxes_for_sale;
      
          if (boxes_in_warehouse < boxes_for_sale) {
              cout << "No, Enter quantity of the boxes for sale: " << flush;
              cin >> boxes_for_sale;
          }
      
          boxes_in_warehouse -= boxes_for_sale;
      
          while (boxes_in_warehouse > 0) {
              cout << "You have " << boxes_in_warehouse << " left" << endl;
              cout << "Please enter quantity of the boxes for sale again" << endl;
              cin >> boxes_for_sale;
              boxes_in_warehouse -= boxes_for_sale;
          }
      
          cout << "we are sold out" << endl;
          return 0;
      }
      

      应该运行到 box_in_warehouse == 0 的代码意味着售罄。

      如果你能优化我最后的代码,我会很高兴看到你的版本。

      【讨论】:

      • 需要注意的三件事:这并不是真正的答案。作为您问题的附录,它会更好。其次,请注意,您不会在 while 循环中测试 boxes_for_sale 是否小于或等于 boxes_in_warehouse,从而允许一些非常不可靠的结果。第三,没有一个cin &gt;&gt; s 被测试以确保用户输入有效的数字。用户可以输入一个非数字并破坏您的程序,可能会将其困在一个无限循环中,但是当 cin 处于错误状态时读取的结果是不确定的。
      • 感谢 cmets,这对我很有用。如何测试 cin >> 的有效数字?
      • 请参阅Why would we call cin.clear() and cin.ignore() after reading input? 了解一个很好的示例以及如何在输入错误后进行清理。 while (!cin &gt;&gt; boxes_in_warehouse) {cout &lt;&lt; "bad input. Try again"&lt;&lt;endl; cin.clear(); cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');} 会一直询问一个号码,直到用户提供一个号码。它仍然有一些问题(用户仍然可以输入不可能的长数字和数字,然后是废话)但应该涵盖您需要的内容
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2016-03-18
      • 2014-08-09
      • 2021-03-01
      • 1970-01-01
      相关资源
      最近更新 更多