【发布时间】:2018-07-03 15:00:52
【问题描述】:
为什么这个do while循环会无限执行?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
unsigned int base;
cout << "Base: ";
cin >> base;
for (int i = 1; i <= base; i++) {
int j = 0;
do {
cout << "#";
j++;
} while (j = i);
cout << endl;
};
system("pause");
// keep on building up until you reach 'base'
return 0;
}
我真的很困惑。这个程序应该创建一个像这样的三角形
#
##
###
(用户输入底部数字,因此在本例中基数 = 3) 有人帮忙解决我的菜鸟错误吗?
【问题讨论】:
-
是否应该说:
} while ( j == i );而不是j = i?
标签: c++ loops infinite-loop do-while