Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c++ - FOR LOOP无法遍历存储购买交易的对象向量(FOR LOOP fails to iterate through the object vector where purchase transactions are stored)

I'm unsure why when more than one transaction has been made the FOR LOOP won't iterate through the vector of "buyers".

(我不确定为什么进行多次交易后,FOR LOOP不会遍历“买方”的向量。)

Am I missing something simple?

(我是否缺少简单的东西?)

I've spent the last few hours trying to fix this but to no avail.

(我花了最后几个小时尝试解决此问题,但无济于事。)

If any one could possibly help with my problem I'd be eternally grateful.

(如果有人可以帮助解决我的问题,我将永远感激不已。)

#include <iostream>
#include <iomanip>
#include <vector>
#include <cctype>

using namespace std;
//This class is for the car details that the user can purchase.
class cCar {
private:
    string _sName;
    double _dPrice;

public:
    cCar(string s, double d)
    {
        _sName = s;
        _dPrice = d;
    }
    string getName() { return _sName; }
    double getPrice() { return _dPrice; }
};
//This is where all the car detail purchases are created and stored in the object 'carlist'
vector<cCar> CarDatabase(vector<cCar>& car_list)
{
    car_list.push_back(cCar("Blue Nissan Skyline", 1000));
    car_list.push_back(cCar("Red Mini", 3000));
    car_list.push_back(cCar("Black Land Rover", 4000));
    car_list.push_back(cCar("Beatle", 9000));
    car_list.push_back(cCar("Ferrari", 300000));
    return car_list;
}
//This class stores the user's transactions
class Finance {
private:
    string _sUserName;
    double _dCostOfCar;
    string _sChosenCar;
    int _iFinancePlan;
    double _dDepositedAmount;
    double _dMonthlyPayments;
    double _dTotalLeftToPay;

public:
    Finance(string sName, double dCostOfCar, string sChosenCar, int iFinancePlan, double dDepositedAmount, double dDMonthlyPayments, double dTotalLeftToPay)
    {
        _sUserName = sName;
        _dCostOfCar = dCostOfCar;
        _sChosenCar = sChosenCar;
        _iFinancePlan = iFinancePlan;
        _dDepositedAmount = dDepositedAmount;
        _dMonthlyPayments = dDMonthlyPayments;
        _dTotalLeftToPay = dTotalLeftToPay;
    }
    string getUserName() { return _sUserName; }
    double getdCostOfCar() { return _dCostOfCar; }
    string getChosenCar() { return _sChosenCar; }
    int getFinancePlan() { return _iFinancePlan; }
    double getDepositAmount() { return _dDepositedAmount; }
    double getMonthlyAmount() { return _dMonthlyPayments; }
    double getTotalLeftToPay() { return _dTotalLeftToPay; }
};

//1. This displays the car menu items.
void display_menu(vector<cCar>& car_list)
{
    cout << "
MENU";
    for (int iCount = 0; iCount != car_list.size(); iCount++) {
        cout << "
" << iCount + 1 << ". " << car_list[iCount].getName();
        cout << "
Price: £" << car_list[iCount].getPrice();
        cout << "
";
    }
}

//This procedure proccesses the user's selection and all information regarding price and name of car are then transferred to transaction variables.
void selectedCar(vector<cCar>& car_list, string& sNameOfChosenCar, double& dCostOfChosenCar)
{

    int iSelectionFromMenu = -1;

    do {
        cout << "
Choose a car that you'd wish to buy from the menu (1 - " << car_list.size() << "): ";
        cin >> iSelectionFromMenu;
        if (iSelectionFromMenu > 0 && iSelectionFromMenu <= car_list.size()) {
            sNameOfChosenCar = car_list[iSelectionFromMenu - 1].getName();
            dCostOfChosenCar = car_list[iSelectionFromMenu - 1].getPrice();
        }
        else {
            cout << "
Please enter valid number!";
            iSelectionFromMenu = -1;
        }
    } while (iSelectionFromMenu == -1);
}
//This procedure gets from the user their preferred finance plan through their input.
void FinanceLength(int& iFinanceLength)
{
    do {
        cout << "
How long do you wish for your finance plan to last? (1 - 4 years): ";
        cin >> iFinanceLength;
        if (iFinanceLength < 0 || iFinanceLength > 4) {
            cout << "
Oops, try again! Please enter between 1 - 4!";
        }
    } while (iFinanceLength < 0 || iFinanceLength > 4);
}

//This procedure gets the user's deposit.
void DepositMoney(double& dDepositAmount)
{
    do {
        cout << "
Enter deposit amount (minimum £500 accepted): £";
        cin >> dDepositAmount;
        if (dDepositAmount < 500) {
            cout << "
Try again! Deposit an amount greater than or equal to £500.";
        }
    } while (dDepositAmount < 500);
}

//This function calculates the amount of money the user has to pay after deposit, added tax and charge percentage of 10%
double TotalLeftToPay(double iFinanceLength, double dDepositAmount, double dCostOfChosenCar)
{
    double dChargePercentage = 0.10;
    double dTotalLeftToPay = dCostOfChosenCar + (dCostOfChosenCar * dChargePercentage) - dDepositAmount + 135;
    return dTotalLeftToPay;
}

//This calculates monthly payments.
double MonthlyPayments(double dTotalLeftToPay, int iFinanceLength)
{
    double dMonthlyPayments = (dTotalLeftToPay / iFinanceLength) / 12;
    return dMonthlyPayments;
}
//This asks the user whether they'd like to restart the application.
void RestartOptions(char& cOption, bool& bExit)
{
    do {
        cout << "
Do you wish to make another purchase? (y/n): ";
        cin >> cOption;
        cOption = toupper(cOption);
        switch (cOption) {
        case 'Y':
            bExit = false;
            break;
        case 'N':
            bExit = true;
            break;
        default:
            cout << "Sorry, that's an invalid input, please try again!";
            continue;
        }
    } while (cOption != 'y' && cOption != 'Y' && cOption != 'n' && cOption != 'N');
}

//This string function returns either year or years (plural)
string YearOrYears(int iFinanceLength)
{
    return (iFinanceLength > 1) ? "years" : "year";
}

//This displays receipt of the user's transaction.
//HERE IS WHRERE I "M STRUGGLING TO ITERATE
void Receipt(const string& sUserName, const int& iFinanceLength, const double& dDepositAmount, char cOption, bool& bExit, const string& sNameOfChosenCar, const double& dCostOfChosenCar, vector<Finance>& buyers)
{
    double dTotalLeftToPay = TotalLeftToPay(iFinanceLength, dDepositAmount, dCostOfChosenCar);
    double dMonthlyPayments = MonthlyPayments(dTotalLeftToPay, iFinanceLength);
    buyers.push_back(Finance(sUserName, dCostOfChosenCar, sNameOfChosenCar, iFinanceLength, dDepositAmount, dMonthlyPayments, dTotalLeftToPay));
    for (int iCount = 0; iCount != buyers.size(); iCount++) {
        cout << "
Receipt for: " << buyers[iCount].getUserName() << ". ";
        cout << "
You have chosen " << buyers[iCount].getChosenCar() << ".";
        cout << "
Your finance plan timescale is " << buyers[iCount].getFinancePlan() << " " << YearOrYears(iFinanceLength) << ".";
        cout << "
You've deposited £" << buyers[iCount].getDepositAmount() << ".";
        cout << "
Total left to pay: £" << buyers[iCount].getTotalLeftToPay();
        cout << "
Monthly Payments: £" << buyers[iCount].getMonthlyAmount();
        cout << "
";
    }

    RestartOptions(cOption, bExit);
}

//This asks the user whether they're happy with the options of they've chosen.
void AcceptDeclineOptions(string& sUserName, int& iFinanceLength, double& dDepositAmount, bool& bExit, string& sNameOfChosenCar, double& dCostOfChosenCar, vector<Finance> buyers)
{
    char cOption = 0;
    do {
        cout << "
Confirm finance plan (y/n): ";
        cin >> cOption;
        cOption = toupper(cOption);
        if (cOption == 'Y' || cOption == 'N') {
            if (cOption == 'Y') {
                Receipt(sUserName, iFinanceLength, dDepositAmount, cOption, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
            }
            else {
                RestartOptions(cOption, bExit);
            }
        }
        else {
            cout << "
Sorry, that's not a valid command.";
        }
    } while (cOption != 'Y' && cOption != 'N');
}

int main()
{
    bool bExit = false;
    int iFinanceLength = 0;
    double dDepositAmount = 0;
    string sNameOfChosenCar = "";
    double dCostOfChosenCar = 0;
    vector<cCar> car_list;
    CarDatabase(car_list);
    vector<cCar> car_purchases;
    vector<Finance> buyers;
    do {
        cout << "Welcome!";
        string sUserName = "";
        cout << "
Enter your name: ";
        cin >> sUserName;
        display_menu(car_list);
        selectedCar(car_list, sNameOfChosenCar, dCostOfChosenCar);
        FinanceLength(iFinanceLength);
        DepositMoney(dDepositAmount);
        AcceptDeclineOptions(sUserName, iFinanceLength, dDepositAmount, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
    } while (bExit == false);
}
  ask by George Bradley translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...