加法运算符没有影响
addition operator has no affect
我正试图在for循环中添加两个浮点,它告诉我"+"没有影响。我试图让它解析两个范围(开始速率和结束速率)(1和2)和1+.25的每个增量(.25)。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;
这是因为begrate+inc对begrate的值没有影响。+运算符与++运算符不同。必须将结果分配给某个对象才能产生效果。你想要的是:
begrate = begrate + inc
或
begrate += inc
您可以使用+=而不是+,因为这会将begrate
设置为begrate+inc
。更好的解决方案是有一个临时循环变量,它开始等于begrate,然后递增
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(float j = begrate;j<endrate;j+=inc){
cout << "Test " << j << endl;
}
}
Just replace the following line
for(begrate;begrate<endrate;begrate+inc){
with
for(begrate;begrate<endrate;begrate+=inc){
注意这里的起始值*+=*inc
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 在 c++ 中是否有办法使用点运算符而不是 -> 来表示 pSomePointer->pValue->nSubvalue
- 重载运算符有地址吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 这里的 = 运算符有什么用法?
- 有条件地将默认参数传递给函数(使用"?"运算符)
- 此分配对速度有影响吗?
- 假设我有作业运算符,我的复制构造函数是this = obj可以
- 仅当模板参数有重载运算符时
- 等号在大括号初始化中是否有影响?例如。 'T a = {}'与'T a{}'
- 完成的线程是否对 winapi 中的主程序有影响
- 在 c++ 中使用 "this" 关键字对性能有影响吗?
- 额外的继承对对象结构或实例化有影响吗?
- 当do额外的括号有影响时,除了运算符优先级
- Array2的变化对Array(表)有影响
- try-catch对char**返回值有影响
- 有没有办法找出一个类是否有重载运算符