【发布时间】: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