我试图在for循环中添加两个浮点数,它告诉我' +'没有影响。我试图让它解析两个范围的每个增量(.25)(生成和结束)(1和2)和1 + .25不能正常工作,我得到一个无限循环
float begrate,endrate,inc,year=0;
cout << "Monthly Payment Factors used in Compute Monthly Payments!" << endl;
cout << "Enter Interest Rate Range and Increment" << endl;
cout << "Enter the Beginning of the Interest Range: ";
cin >> begrate;
cout << "Enter the Ending of the Interest Range: ";
cin >> endrate;
cout << "Enter the Increment of the Interest Range: ";
cin >> inc;
cout << "Enter the Year Range in Years: ";
cin >> year;
cout << endl;
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(begrate;begrate<endrate;begrate+inc){
cout << "Test " << begrate << endl;
}
}
system("pause");
return 0;
答案 0 :(得分:7)
那是因为begrate + inc对begrate的值没有影响。 +运算符与++运算符不同。您必须将结果分配给具有效果的内容。你想要的是:
begrate = begrate + inc
或者
begrate += inc
答案 1 :(得分:4)
您可以使用+ =代替+,因为这会将begrate
设置为begrate+inc
。更好的解决方案是使一个临时循环变量开始等于生成然后递增它。
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(float j = begrate;j<endrate;j+=inc){
cout << "Test " << j << endl;
}
}
答案 2 :(得分:3)
Just replace the following line
for(begrate;begrate<endrate;begrate+inc){
with
for(begrate;begrate<endrate;begrate+=inc){
请注意这里的生命* + = * inc