我试图在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;
Run Code Online (Sandbox Code Playgroud)
那是因为begrate + inc对begrate的值没有影响.+运算符与++运算符不同.您必须将结果分配给具有效果的内容.你想要的是这个:
begrate = begrate + inc
Run Code Online (Sandbox Code Playgroud)
要么
begrate += inc
Run Code Online (Sandbox Code Playgroud)