C++循环未运行时
C++ while loop not running
my while 循环不会在我设置的条件下运行。
该计划的目的是使用存款金额、利率和目标金额确定存款金额到期所需的年限。
程序在输入目标金额后停止,除非我将 while 语句从 <= 更改为>=,在这种情况下,它会运行循环,但返回设置为 100 或 1000 等的年数......
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
提前感谢!
问题是一个不幸的后缀:
while (balance <= target);
// ^
这相当于:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
只需删除分号即可。
相关文章:
- 我可以根据用户输入在运行时生成"循环"吗?
- 类方法 - 数据结构中 For 循环的运行时错误
- 为什么程序运行时我的第二个循环不执行?
- 为什么我的代码在运行时保持循环
- 骰子滚轮随机循环每次运行时都保持相同的值
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 查找涉及 while 循环的运行时函数
- 在接收输入时运行后台循环 (C++)
- 默认情况下,C++ 开关运行时循环
- Bash 代码,仅在当前可执行文件完成运行时在循环中运行可执行文件
- OPENCV C .在for循环中在不同的图像像素上绘制一个圆圈(应在每个循环运行时将图像打开)
- 是否有一种方法可以在运行时停止循环重复一次不止一次
- 在循环中使用lower_bound函数会给出运行时错误
- 如何在每次运行时将数字从循环中提取出来
- Windows 生物识别服务在调用 WinBioCaptureSample 时循环运行 SensorAdapterSta
- 每当我的程序运行时,使用SRAND(TIME(NULL))都会给出相同的输出(不在循环中,播种有足够大的值)
- 为什么当循环再次运行时,此输出"请重试"?
- 循环运行时错误
- 循环和运行时错误中的C 变量声明
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误