【问题标题】:calender running infinite loop [closed]日历运行无限循环[关闭]
【发布时间】:2014-07-29 06:50:46
【问题描述】:

输出日历的代码工作正常,但没有显示每个月所需的空格数。假设如果一月在星期五结束,二月应该从星期六开始。

为了记录这一点,我添加了一个 line 变量,该变量在新月份开始时通过查看开始日来提供所需的空格数,即sd。

所以每个月之后我都必须将line 初始化为 1 以便为下个月腾出空间,但将 line 初始化为 1 正在运行无限循环。

while (month <= 12)
{
  if (month == 2)
    days = 28;

  else if (month == 4 || month == 6 || month == 9 || month == 11)
    days = 30;

  else days = 31;

  cout << endl << endl << endl;

  if (month == 1)
    cout << "      JANUARY 20XX    \n";

  else if (month == 2)
    cout << "      FEBRUARY 20XX    \n";

  else if (month == 3)
    cout << "      MARCH 20XX    \n";

  else if (month == 4)
    cout << "      APRIL 20XX    \n";

  else if (month == 5)
    cout << "      MAY 20XX    \n";

  else if (month == 6)
    cout << "      JUNE 20XX    \n";

  else if (month == 7)
    cout << "      JULY 20XX    \n";

  else if (month == 8)
    cout << "      AUGUST 20XX    \n";

  else if (month == 9)
    cout << "      SEPTEMBER 20XX    \n";

  else if (month == 10)
    cout << "      OCTOBER 20XX    \n";

  else if (month == 11)
    cout << "      NOV 20XX    \n";

  else if (month == 12)
    cout << "      DEC 20XX    \n";

  cout << "-   -   -   -   -   -   -\n";
  cout << "M   T   W   T   F   S   S\n";
  cout << "-   -   -   -   -   -   -\n";

  while (j < days) {
    for (i = 0; i < 7
         && j <= days; i++) { //i from 0to 6 for 7 days.j from 1 to no. of
      //days in the month
      if ((line == 1) && (i < sd))     //line =1 so that space is only in first line
        cout << "    ";

      else {
        cout << j << "  ";
        j++;

        if (i == 6) {
          cout << endl;
          line++;
        }
      }
    }
  }

  if (i == 7)
    sd = 1;

  else sd = i + 1;

  cout << sd;
  month++;
  j = 1;
  i = 0;
  line = 1; //infinite loop here!On removing line=1, it works fine except spaces.
}

【问题讨论】:

  • 你没听说过数组吗?甚至是开关?

标签: c++ controls infinite-loop


【解决方案1】:

我怀疑变量没有被初始化。 This works fine, as you say, just with the incorrect spaces

您尚未发布MCVE,但请务必声明并初始化您的循环变量,例如:

int month = 0;
int line = 0;
int days = 0;
int j = 0;
int i = 0;
int sd = 0;

【讨论】:

  • 我同意这一点。由于缺少 MCVE,我不得不自己构建,因此,我无法确定我的版本(顺便说一句,它似乎可以工作)与 OP 的版本相同。
  • @DavidFrye。同样在这里,我所做的唯一假设是循环变量的类型,只需像往常一样将它们设置为0,并且看起来一切正常。从代码的外观来看,我认为这很像我见过的其他“变量初始化问题”。
【解决方案2】:

在下面的代码摘录中:

while (j < days) {
for (i = 0; i < 7 && j <= days; i++) { 
  if ((line == 1) && (i < sd))     
    // The above if statement is false if 'line != 1' or 'i >= sd'.
  else {
    // 'line' is incremented in here. Hence this code will only be
    // executed if the preceding if-statement is terminated via the 'i < sd' condition.
  }

我怀疑i 永远不会变成&gt;= sd。 sd的值是多少?

如果sd 大于或等于7,则有问题。

for (i = 0; i &lt; 7 &amp;&amp; j &lt;= days; i++) 将在i &lt; sd 条件为假之前在i &lt; 7 终止,因为line 卡在1 和i 由于外部@ 而永远不会大于6 987654334@ 终止条件。

j &lt;= days 将保持为真,因为递增 j 的代码永远不会执行。

【讨论】:

  • 如果您对A &amp;&amp; B 有条件,则当!A 或!B 时它变为假。因此,对于if 和for,您所说的一切都不成立。
  • @Svalorzen,是的,但我的论点是,在外部 for-loop 条件 i &lt; 7 变为假之前,line == 1 和 i &lt; sd 都不会变为假。因此line 和j 永远不会增加。
猜你喜欢
  • 2014-01-28
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2020-04-26
相关资源
最近更新 更多