代码有效 数学不行
Code WORKS Math DOESN'T
我的代码编译得很好,但我使用的数学公式没有提供正确的结果。我需要计算所有 3 个月的余额、提取和利息。我还需要验证用户的输入。出于这些目的,我使用嵌套循环。如果您发现我的错误,请告诉我。谢谢可爱的人!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "nt***ERROR " << balance << " ***";
cout << "*** Choice must be positive***n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
你定义了很多不需要的额外变量。此外,您的利率可能是百分比而不是十进制数字,即 10% = 0.1。此外,您的monthInterest
取平均值,然后应用利息。我写了这个,它似乎有效。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}
相关文章:
- GCC对可能有效的代码抛出init list生存期警告
- 这是什么代码?为什么它有效?C++
- 为什么此代码片段有效?如何取消引用空点?
- 使用C++字谜代码仅在有时有效
- 为什么以下代码在在线 ide(gcc 7.2.0) 上有效,但在 ubuntu 上出现错误?
- 为什么第二个代码有效而第一个代码无效?
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?
- 代码 C++ 以添加前缀关键字以使其有效
- 为什么这个简短的模板代码片段有效
- 如何有效地处理Teamcenter ITK C++代码中的错误
- 这是有效的C++代码吗?(在声明范围之外使用本地类型)
- 以有效的方式从 Node .js运行 C 或C++代码
- 使用std ::函数时运行时异常.他们为什么不指向有效的代码
- 为什么认为参考文献在C 底漆第5个中没有变化,以下代码有效
- (C++)比较两段代码,一段有效,一段无效,无法找出区别
- 为什么这段代码在Visual Studio中有效,但在gcc中失败
- 通用lambda,继承和尾随返回类型:此有效代码
- 正在使用 std::time 和 std::srand 有效代码
- 用于确定两个数字中哪个较大的有效代码
- 有效代码无法通过visual studio 2015编译(std函数错误?)