我的教授是说,while循环运行条件是n> = 1的循环。但是我没有给变量n赋任何值,因此取决于它的“默认”值,循环可能不会被输入。而且我不确定如何解决他在说什么!?如何保证被输入(C++)
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int n, count;
double sum;
while (n >=1)
{
cout << "Enter a positive integer N (<1 to stop): ";
cin >> n;
sum = 0;
for (count = 1; count <= n; count++)
sum = sum + (1.0/count);
cout << "Sum = " << sum << endl;
}
cout << "Bye! ";
return 0;
}
ñ,因为它不是初始化 – ivan
@ivan未初始化变量将永远不会进入可以在C++中的任何值,因此您的评论是不正确的(参见下面的答案)。 – jofel
@jofel它在Visual Studio中运行代码时引发运行时异常 – novice