日历运行无限循环
calender running infinite loop
用于输出日历的代码工作正常,但它没有显示每个月所需的空格数。假设一月在星期五结束,二月应该从星期六开始。
为了保持计数,我添加了一个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 Sn";
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.
}
我怀疑变量没有被初始化。正如您所说,这很好,只是使用了不正确的空格
你还没有发布一个MCVE,但一定要声明和初始化你的循环变量,如:
int month = 0;
int line = 0;
int days = 0;
int j = 0;
int i = 0;
int sd = 0;
在下面的代码摘录中:
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
永远不会变成>= sd
。sd
的值是多少?
如果sd
大于或等于7
,则有问题。
for (i = 0; i < 7 && j <= days; i++)
将在if ((line == 1) && (i < sd))
的i < sd
条件为假之前在i < 7
上终止,因为line
被卡在1
上,i
由于for-loop
的外部终止条件而永远不会大于6。
j <= days
将保持为真,因为增加j
的代码永远不会执行。
相关文章:
- 过载'operator new'如何导致无限循环?
- 运行无限循环的最小二叉树问题
- 作为无限循环运行
- 字母输入运行无限循环
- 插入无限循环运行的单向链表
- 由于next_permute而运行无限循环
- boost::thread应该在无限循环中运行,并等待没有互斥锁的新输入
- 从C++应用程序运行批处理脚本,并检查它是否具有无限循环
- 代码在小输入时运行良好,但在大输入时就像一个无限循环
- 我认为做而正在进入无限循环.或数组.(运行时错误)
- 日历运行无限循环
- 终止一个在无限循环中运行的std::线程
- 如何运行一个无限循环
- 使用两个线程运行和中断一个无限循环
- 来自cmath的函数生成运行时无限循环(没有这样的文件或目录)
- 为什么我得到无限循环只有线程# 0后,所有5个线程运行多线程c++用餐哲学家谜语
- 我总是在运行这个程序时陷入无限循环
- 当主线程在无限循环中运行时,用QTcpSocket::write写入的字节没有被服务器接收
- c++:程序在运行函数时进入无限循环
- 如何在无限循环中运行线程