Reputation: 671
I am trying to add two floats in a for loop and its telling me '+' has no affect. I am attempting to make it parse through each incrememnt (.25) of the two ranges (begrate and endrate) (1 and 2) and 1+.25 is not working correctly and I get an infinite loop
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;
Upvotes: 0
Views: 148
Reputation: 246
You could use += instead of +, as this will set begrate
to begrate+inc
. The better solution would be to have a temporary loop variable that starts equal to begrate then increment it.
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(float j = begrate;j<endrate;j+=inc){
cout << "Test " << j << endl;
}
}
Upvotes: 4
Reputation: 6514
Just replace the following line
for(begrate;begrate<endrate;begrate+inc){
with
for(begrate;begrate<endrate;begrate+=inc){
notice the begrate*+=*inc here
Upvotes: 3
Reputation: 6846
That's because begrate+inc has no effect on the value of begrate. The + operator is not like the ++ operator. You must assign the results to something to have an effect. What you wanted is this:
begrate = begrate + inc
Or
begrate += inc
Upvotes: 7