贷款和利率程序c++
Loan and interest rate program c++
我正在尝试编写一个程序,以便用户输入金额、利率和付款次数。
假设代码输出如下内容:
Principal: 120000
Interest rate: 10
Number of payments: 12
No. Balance Instalment Interest Total payment
----------------------------------------------------------------
1 120000.00 10000.00 1000.00 11000.00
2 110000.00 10000.00 916.67 10916.67
3 100000.00 10000.00 833.33 10833.33
4 90000.00 10000.00 750.00 10750.00
5 80000.00 10000.00 666.67 10666.67
6 70000.00 10000.00 583.33 10583.33
7 60000.00 10000.00 500.00 10500.00
8 50000.00 10000.00 416.67 10416.67
9 40000.00 10000.00 333.33 10333.33
10 30000.00 10000.00 250.00 10250.00
11 20000.00 10000.00 166.67 10166.67
12 10000.00 10000.00 83.33 10083.33
----------------------------------------------------------------
120000.00 6500.00 126500.00
但是我编写的代码输出如下:
Number of payments: 12
No. Balance Instalment Interest Total payment
----------------------------------------------------------------
----------------------------------------------------------------
000Program ended with exit code: 0
这是我目前掌握的代码。
#include <iostream>
using namespace std;
class Loan
{
public:
Loan(double upphaed, double vextir, int afborganir);
void displaySchedule();
private:
void pay(int i);
double payments();
double interest(int i);
double Balance(int i);
double sumOfInstalments;
double sumOfInterest;
double total;
double principal;
double interestRate;
int numOfPayments;
};
Loan::Loan(double upphaed, double vextir, int afborganir)
{
upphaed = principal;
vextir = interestRate;
afborganir = numOfPayments;
sumOfInstalments = 0.0;
sumOfInterest = 0.0;
total = 0.0;
}
void Loan::pay(int i)
{
sumOfInstalments += payments();
sumOfInterest += interest(i);
total += payments() + interest(i);
}
double Loan::payments()
{
return principal / numOfPayments;
}
double Loan::interest(int i)
{
return Balance(i) * (interestRate / 12) / 100;
}
double Loan::Balance(int i)
{
return principal - (i - 1) * payments();
}
void readData(double& principal, double& interestRate, int& numOfPayments)
{
cout << "Principal: ";
cin >> principal;
cout << "Interest rate: ";
cin >> interestRate;
cout << "Number of payments: ";
cin >> numOfPayments;
}
void Loan::displaySchedule()
{
cout << "No. Balance Instalment Interest Total payment" << endl;
cout << "----------------------------------------------------------------" << endl;
for (int i = 1; i <= numOfPayments; i++)
{
cout << i << " " << Balance(i) << " " << payments() << " " << interest(i) << " " << (payments() + interest(i));
pay(i);
}
cout << "----------------------------------------------------------------" << endl;
cout << sumOfInstalments << sumOfInterest << total;
}
int main()
{
double amount, interestRate;
int numPayments;
readData(amount, interestRate, numPayments);
Loan myLoan(amount, interestRate, numPayments);
myLoan.displaySchedule();
return 0;
}
您的构造函数正在以错误的方式进行操作:
Loan::Loan(double upphaed, double vextir, int afborganir)
{
upphaed = principal;
vextir = interestRate;
afborganir = numOfPayments;
sumOfInstalments = 0.0;
sumOfInterest = 0.0;
total = 0.0;
}
代替:
Loan::Loan(double upphaed, double vextir, int afborganir)
{
principal == upphaed;
interestRate = vextir;
numOfPayments = afborganir;
sumOfInstalments = 0.0;
sumOfInterest = 0.0;
total = 0.0;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址