2017-09-02 95 views
2

我正在寫一個基本的債務計算器來練習我的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"; 
} 

這裏是我完整的代碼,對改善計劃的任何額外的評論也appriciated:

Debt.h

#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 

Debt.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 Limit\n"; 

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) Yes\n" 
      << "2) No\n"; 
     std::cin >> check; 
    } 
} 
newList.calcPayoff(); 
newList.printDebt(); 

}

+1

(自動I:vectDebt)爲我創建臨時副本,您很可能希望使用(自動&I:vectDebt) – moggi

+0

我想它有一些東西需要臨時變量,但WASN不知道在哪裏。謝謝! – Kunigaz

回答

6
for (auto i : vectDebt) { 
    //... 
} 

在這個循環中,i將每個項目的副本vectDebt。對i的任何更改都不會反映在vectDebt中的項目中。你想要的是參考vectDebt中的每個項目。要做到這一點你的循環改變

for (auto& i : vectDebt) { 
    //... 
} 
相關問題