为什么我的矢量删除数据
Why is my vector dropping its data?
我正在写一个基本的债务计算器来练习我的C ,但我的一个向量一直在删除其值。
包含向量的结构:
struct debt
{
std::string institution, debtType;
double creditLimit, balance, intrest, debtRatio, minMonthly;
int monthsToPayOff;
std::vector <double> monthlyBalance; //this vector is giving me trouble
};
我调用一个函数,该功能每月存储每月余额减去矢量中的每月付款。
for (auto i : vectDebt)
{
double tempBalance = i.balance;
while (tempBalance > 0)
{
i.monthlyBalance.push_back(tempBalance); //this seems to work fine
tempBalance -= i.minMonthly;
}
if (i.monthlyBalance.size() > vectMonths.size())
vectMonths.resize(i.monthlyBalance.size());
}
Visual Studio调试器显示,这些值已正确存储在上述代码中。然后,我尝试将值打印到.csv文件,但到那时,向量已经空了。
//print the payoff balances in debt.csv
for (auto i: vectDebt)
{
for (int j = 0; j != i.monthlyBalance.size(); j++)
{
fileout << i.monthlyBalance[j] << ","; //the debugger shows monthlyBalance has size = 0
}
fileout << "n";
}
这是我的完整代码,有关改进程序的任何其他评论也将被应用:
债务
#pragma warning(disable : 4996) //diable localtime warning
#ifndef DEBT_H
#define DEBT_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
struct debt
{
std::string institution, debtType;
double creditLimit, balance, intrest, debtRatio, minMonthly;
int monthsToPayOff;
std::vector <double> monthlyBalance;
};
class Debt
{
private:
time_t t = time(NULL);
tm *timeinfo = localtime(&t);
const std::string months[12] = { "January","February","March","April","May","June","July",
"August","September","October","November","December" };
std::ofstream fileout;
std::vector <debt> vectDebt;
std::vector <std::string> vectMonths;
public:
Debt() { }
void addDebt(debt temp)
{
vectDebt.push_back(temp);
}
std::string returnMonth(int month)
{
std::string currentMonth = months[month - 1];
return currentMonth;
}
void calcPayoff();
void printDebt();
void printPayoff();
~Debt() { }
};
#endif //DEBT_H
债务.cpp
#include "Debt.h"
void Debt::calcPayoff()
{
for (auto i : vectDebt)
{
double tempBalance = i.balance;
while (tempBalance > 0)
{
i.monthlyBalance.push_back(tempBalance);
tempBalance -= i.minMonthly;
}
if (i.monthlyBalance.size() > vectMonths.size())
vectMonths.resize(i.monthlyBalance.size());
}
}
void Debt::printDebt()
{
fileout.open("debt.csv");
fileout << "Institution(type),Current Balance,Intrest,Min Monthly Payment,Credit Limitn";
for (auto i : vectDebt)
{
fileout << std::fixed << std::setprecision(2) << i.institution << "("
<< i.debtType << "),"
<< i.balance << ","
<< i.intrest << ","
<< i.minMonthly << ","
<< i.creditLimit << "n";
}
printPayoff();
fileout.close();
}
void Debt::printPayoff()
{
int monthCounter = timeinfo->tm_mon;
fileout << "nBalance per Month until paid off:n";
for (int i = 0; i != vectMonths.size(); i++)
{
vectMonths.at(i) = months[monthCounter % 12];
monthCounter++;
}
for (auto i : vectMonths)
fileout << i << ",";
fileout << "n";
//print the payoff balances in debt.csv
for (auto i: vectDebt)
{
for (int j = 0; j != i.monthlyBalance.size(); j++)
{
fileout << i.monthlyBalance[j] << ",";
}
fileout << "n";
}
}
源文件:
#include "Debt.h"
void main() {
Debt newList;
int check = 1;
std::string institution = "",
debtType = "";
double balance = 0,
intrest = 0,
creditLimit = 0,
minMonthly = 0;
std::cout << "This program will ask you to enter each of your debts institution(bank name), "
<< "debt type(loan, credit, car), current balance, interest rate, credit limit, and minimum monthly payment.n";
while (check == 1) {
debt temp;
if (check == 1) {
std::cout << "Enter name of institution: ";
std::cin >> temp.institution;
std::cout << "Enter debt type (loan, credit, car...): ";
std::cin >> temp.debtType;
std::cout << "Enter current balance: ";
std::cin >> temp.balance;
std::cout << "Enter inrest rate for this debt: ";
std::cin >> temp.intrest;
std::cout << "Enter credit limit for this debt: ";
std::cin >> temp.creditLimit;
std::cout << "Enter the minimum monthly payment for this debt: ";
std::cin >> temp.minMonthly;
temp.debtRatio = 100*(temp.balance/temp.creditLimit);
newList.addDebt(temp);
std::cout << "Add another debt? n"
<< "1) Yesn"
<< "2) Non";
std::cin >> check;
}
}
newList.calcPayoff();
newList.printDebt();
}
for (auto i : vectDebt) {
//...
}
在此循环中,i
将是vectDebt
中每个项目的副本。i
的任何更改都不会反映在vectDebt
中的项目中。您想要的是vectDebt
中每个项目的参考。为此,将循环更改为
for (auto& i : vectDebt) {
//...
}
相关文章:
- 从 Typedef 数据类型中删除常量
- 从输入中删除重复项,而不使用任何数据结构
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 从自定义数据类型向量中删除重复元素
- 如何使用C++删除/更新txt文件中的特定数据
- 如何使用C++删除/更新txt文件中的特定数据?
- 如何从 txt 文件中删除数据/内容
- 如何根据偏移从文件中删除数据
- 如何在返回一次数据后从类实例中删除数据
- 凯撒密码删除数据
- Winsock:从套接字中删除数据
- 为什么我的矢量删除数据
- 设置/删除窗口子类时,正确传递/删除数据
- 双重删除数据不会崩溃
- 具有非常快速的内>数据查找和快速反向查找(搜索/插入/删除数据)的压缩字典?
- 当构造函数抛出异常时,如何删除数据成员的内存
- 从节点中删除数据
- 仅重定向子进程stdin,并从stdout和stderr中删除数据
- 删除数据文件中的条目
- 从链表中删除数据