簡體   English   中英

如何將我的 toString function 調用到我的主 function?

[英]How can I call my toString function to my main function?

目前,我的程序循環並允許用戶根據需要多次輸入帳戶信息。 但是,當輸入 2 時,顯示的帳戶信息是最后一次輸入的信息,而不是多個輸入(帳戶)的信息。 我有一種感覺,我需要在某處使用 push.back 將用戶輸入發送到向量但不確定。

CertDeposit::CertDeposit()
{

}
CertDeposit::CertDeposit(double In_Principal, int In_AccountNumber,
    string In_FirstName, string In_LastName,
    double In_InterestRate, double In_DepositYears,
    int In_NumCompounding)
{
    Principal = In_Principal;
    AccountNumber = In_AccountNumber;
    FirstName = In_FirstName;
    LastName = In_LastName;
    InterestRate = In_InterestRate;
    DepositYears = In_DepositYears;
    NumCompounding = In_NumCompounding;
}
double CertDeposit::getAccumulation() const
{
    return Accumulation;
}
double CertDeposit::getInterestAccrued() const
{
    return InterestEarned;
}
const string CertDeposit::toString() const
{
    ostringstream buffer;
    buffer << fixed << showpoint << setprecision(2);
    cout << "Account Number: " << getAccountNumber() << endl
        << "Name: " << getFullName() << endl
        << "Original Deposit: $" << getPrincipal() << endl
        << "Annual Interest rate: " << getAnnualInterestRate() << endl
        << "Number of years of deposit: " << getDepositYears() << endl
        << "Number of times compounded per year: " << getNumCompoundings() << endl
        << "Total accumulation: $" << getAccumulation() << endl
        << "Interest accrued: $" << getInterestAccrued() << endl;
    return buffer.str();
}
// All function starting with word get ONLY Have one line of code!
// That code is just one return statement.
// See Savitch to see how to write get functions.
double CertDeposit::getPrincipal()const
{
    return Principal;
}

const string CertDeposit::getFirstName() const
{
    return FirstName;
}

const string CertDeposit::getLastName() const
{
    return LastName;
}

const string CertDeposit::getFullName() const
{
    return FirstName, LastName;
}

int CertDeposit::getAccountNumber() const
{
    return AccountNumber;
}

double CertDeposit::getDepositYears() const
{
    return DepositYears;
}

int CertDeposit::getNumCompoundings() const
{
    return NumCompounding;
}

double CertDeposit::getAnnualInterestRate() const
{
    return InterestRate;
}

void CertDeposit::setLastName(string In_LastName)
{
    throw "To be completed";
int main()
{
    vector<CertDeposit> CDVec;
    string FirstName, LastName;
    int  AccountNumber, NumCompounding, x = 0, i;
    double Principal, InterestRate, DepositYears, Accumulation, InterestEarned;

    cout << "This program would demo certificate of deposit account in a bank." << endl;

    while (x < 5) {
        cout << "********* Main Menu ***********" << endl;
        cout << "1. Add a new Certificate of deposit account to the CertDeposit Vector:" << endl;
        cout << "2. Print all certificate of deposit accounts to console:" << endl;
        cout << "3. Print all certificate of deposit accounts to an output file" << endl;
        cout << "[User would be asked for the output file full path]:" << endl;
        cout << "4. Sort CertDeposit Vector based on account number in ascending order:" << endl;
        cout << "5. Exit" << endl;
        cin >> x;
        cout << endl;

        switch (x) {
        case 1:
            cout << "Enter account holder's first name:" << endl;
            cin >> FirstName;
            cout << "Enter account holder's last name:" << endl;
            cin >> LastName;
            cout << "Enter account number [whole number only]:" << endl;
            cin >> AccountNumber;
            cout << "Enter amount of original deposit:" << endl;
            cin >> Principal;
            cout << "Enter annual interest rate. For example for 7.2% enter 0.072:" << endl;
            cin >> InterestRate;
            cout << "Enter number of years for certificate of deposit:" << endl;
            cin >> DepositYears;
            cout << "Enter number of times interest is compounded per year" << endl;
            cout << "[whole number only]: ";
            cin >> NumCompounding;
            break;
            CDVec.emplace_back(Principal, AccountNumber, FirstName, LastName,
                InterestRate, DepositYears, NumCompounding);
        case 2:
            for (size_t i = 0; i < CDVec.size(); i += 1)
            {
                CDVec[i].toString();
            }
            //cout << .tostring() << endl;
        case 3:
            cout << "Enter full path to output file: ";
            //cin >> 
            break;
        case 4:
            Sort(CDVec);
            break;
        case 5:
            break;
        }
    }
    system ("pause"); // uncomment for Visual Studio 2017
    return 0;
}

inline bool operator<(const CertDeposit & a, const CertDeposit & b)
{
    return a.getAccountNumber() < b.getAccountNumber();
}
void Sort(vector<CertDeposit>  &CDVec)
{
    sort(CDVec.begin(), CDVec.end());
}

好吧,我想你需要將數據存儲在某個地方,如果你想稍后訪問它們,因為你建議的向量安靜適合這個。 但重要的是,首先您需要構造數據並將它們存儲為對象。

每次迭代時,您必須實例化 CertDeposit object,並且在 case1 輸入數據時,您需要將其添加到 object。 就像是,

 CertDepoObj. Firstname = "Some value";
 CertDepoObj. AccountNo = "Some value";

最后,您將把 object 推入向量中。

為了根據帳號對向量進行排序,您必須編寫一個單獨的比較器 function ,可以將其傳遞給排序 function 來對其進行排序。

  • 案例 1 永遠不會創建 CertDeposit 類型的CertDeposit ,並且您永遠不會將其放入向量中。 這很容易解決。 獲得所需的所有數據后:
CDVec.emplace_back(Principal, AccountNumber, firstName, lastName, 
                   InterestRate, DepositYears, NumCompounding);
// Requires C++11, shouldn't be an issue
// emplace_back will both create a CertDeposit object, and place into the vector
// Note that the arguments to emplace_back are identical to your constructor arguments
  • 案例 2 只打印您最后輸入的任何內容(您正在打印變量,而不是來自CertDeposit object 的信息,絕對不是來自您的向量),因為同樣,您從未真正將信息存儲起來。 也沒有循環,因此即使您將其存儲起來,您也只會打印一項。 您需要打印 function,或為 CertDeposit 重載operator<<() CertDeposit
// pseudo-code(-ish)
for (auto i : CDVec) {
    std::cout << i << '\n';  // Assumes overloaded operator<<()
}

請注意,您的命名不一致。 以大寫字母開頭的名稱通常保留給 class 或類型名稱。 您還可以在駝峰式和下划線之間交替,沒有押韻或理由。 這些東西使你的代碼更難理解。

無論哪種方式,go 都可能出現小問題,如果您只想在一個位置調用它,我看不出您的Sort() function 的意義。 除非您提供一些 function 來比較CertDeposit s(通常通過重載operator<() [切線,如果您重載小於,則應該重載所有比較運算符]),它應該會中斷。 如果 class 是微不足道的,也許它不會,但我覺得它不會編譯。 如果沒有單一的方法來比較 CertDeposits,即不同的比較是可行的,您可以向std::sort()提供 lambda 或者只是在您的 class 中創建不同的friend比較函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM