【问题标题】:Confused about queues, and how to implement them对队列感到困惑,以及如何实现它们
【发布时间】:2016-05-24 19:28:10
【问题描述】:

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

#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(i);

不知道为什么我们会弹出这个错误。

【问题讨论】:

  • 你得到哪个错误?
  • 您还需要考虑许多其他问题。在使用值初始化之前,您在 main 中使用 numofacc
  • “我只是不认为我知道如何准确地工作,即使做了一些研究。” 这不是我们可以解决的科学问题陈述。
  • 在使用库之前为什么不阅读some documentation?哦,我知道,因为你可以在这里转储你的代码,有人会为你修复它。

标签: c++ class object random queue


【解决方案1】:

std::queue 的成员front 不接受任何参数

所以改变

accounts.front(i);

a1 = &(accounts.front());

第二个更正是在你的main() 函数中,你写accounts.emplace(i);.. 我猜你的意思是accounts.emplace(*a1);

最后,您的 account 类中有一些成员函数应该返回某些内容,但您没有返回任何内容。

【讨论】:

  • 谢谢!我仍在尝试完全理解并在我的代码中实现指针,所以这可能是我错过这些的原因。至于成员函数,我意识到我最好把它们当作一个空白,因为它们可以满足我的需要。
  • 好的,很高兴我能帮上忙。顺便说一句,如果我已经解决了你关于这个问题的问题,你可以批准这个答案:-)
  • 为什么在我尝试删除账户(将ID和余额重置为0)时,我在最后重新打印客户信息时没有显示?我使用这个成员函数来做到这一点,但它实际上并没有改变值 {cout
  • @Ploxzx 没有看到你的代码我真的帮不上你,尝试将代码sn-p上传到任何在线代码共享并在你的评论中分享链接
【解决方案2】:

我试过你用我的 IDE Visual Studio community 2015 运行你的代码,我收到了一些错误,在第 32 行,你正在从 int 转换为 float,这可能会导致数据丢失,下一行也是一样(33)。 你想用 accounts.front(i); 做什么?前台功能这样不行。
Front function

【讨论】:

    【解决方案3】:

    这里有一些有问题的代码。

    class account和int之间数据类型的差异:

    队列帐户; account.emplace(i);

    queue::front 参数:

    accounts.front(i);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2019-03-31
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多