对队列感到困惑,以及如何实现它们

时间:2016-02-13 17:36:51

标签: c++ class object random queue

我一直坚持这种创建银行队列的任务。在我需要实现队列部分之前,一切似乎都工作得很好,即使在做了一些研究之后,我也不认为我知道如何正确地工作。

#include <iostream>
#include <string>
#include <CMATH>
#include <vector>
#include <queue>
using namespace std;

#define MAXCUSTOMERS 21

class account
{
private:
    string name;
    int ID;
    float balance;

public:



    string getname(string fn)
    {
        name = fn;
        return name;
    }
    int getID(int)
    {
        ID = rand() % 9000 + 1000;
        return ID;
    }
    int getbalance(int)
    {
        balance = rand() % 10000 + 1800;
        return balance;
    }
    float deposit()
    {
        int amount;
        cout << "Enter amount to be deposited: ";
        cin >> amount;
        balance += amount;
        cout << " Your balance currently stands at: " << balance << endl;
    }
    float withdraw()
    {
        int amount;
        cout << "Enter amount to be withdrawn: ";
        cin >> amount;
        if (balance <= amount)
            cout << "\nInsufficient balance! Operation Cannot be performed!" << endl << endl;
        else
            balance = balance - amount;
            cout << " Your balance currently stands at: " << balance << endl;;
    }
    void closeacc()
    {
        //not quite sure how to approch at the moment
    }
    void createacc()
    {
        // not quite sure how to approach at the moment 
    }
    void printinfo()
    {
        cout << " Name      : " << name << endl;
        cout << " Balance   : " << balance << endl;
        cout << " ID        : " <<  ID << " \n " << endl;
    }
};
int main()
{
    queue<account> accounts;
    vector<account> myvector;
    unsigned int money =0;
    unsigned int id=0;
    int numofacc;
    string name;
    numofacc = rand() % 11 + 10;

    cout << "There are currently : " << numofacc << " people waiting in line." << endl;

    account *a1;

    for (int i = 1; i <= numofacc; i++)
    {
        cout << "Enter your name: " << endl;
        cin >> name;
        a1 = new account;
        a1->getname(name);
        a1->getbalance(money);
        a1->getID(id);  
        myvector.push_back(*a1);
        accounts.emplace(i);
    }
    vector<account> ::iterator it;
    for (it = myvector.begin(); it != myvector.end(); ++it)
    {
        it->printinfo();
    }

    for (int i = 1; i <= numofacc; i++)
    {
        int input;
        accounts.front(i);

        cout << "Please tell me what you would like to do with your account:  \n"
            "Press 1 to Widthdraw money. \n "
            "Press 2 to deposit money . \n"
            "Press 3 to close the account. \n"
            "Press 4 to create a new account. \n" <<endl;
        cin >> input;

        switch (input)
        {
        case 1:
            a1->withdraw();
            accounts.pop();
            break;
        case 2:
            a1->deposit();
            accounts.pop();
            break;
        case 3:
            a1->closeacc();
            accounts.pop();
            break;
        case 4:
            a1->createacc();
            accounts.pop();
            break;
        default:
            cout << "You did not select 1 of the 4 options, you shouldn't be playing with money if you can't understand the basics of chosing a number." << endl;
            accounts.pop();
            break;
        }



    }




}

接收错误是我尝试使用此

获取队列的前端

accounts.front(ⅰ);

不确定为什么会出现这个错误。

3 个答案:

答案 0 :(得分:2)

std::queue名为front的成员并未接受任何争论

所以改变

accounts.front(i);

a1 = &(accounts.front());

第二次更正位于main()函数中,即您编写accounts.emplace(i);的行。我认为您的意思是accounts.emplace(*a1);

最后,你的account类中有一些成员函数应该返回一些东西,但你没有返回任何东西。

答案 1 :(得分:0)

这是一些有问题的代码。

类帐户和int之间的数据类型不一致:

队列帐户; accounts.emplace(ⅰ);

queue :: front参数:

accounts.front(ⅰ);

答案 2 :(得分:-1)

我已尝试使用我的IDE Visual Studio社区2015运行您的代码并且我收到了一些错误,在第32行,您正在从int转换为float,这可能会导致数据丢失,下一行也是如此(33)。 你想用accounts.front(i);做什么?前面的功能不起作用。
Front function

相关问题