gpt4 book ai didi

c++ - 初始化面向对象的数组C++

转载 作者:行者123 更新时间:2023-12-03 07:15:46 25 4
gpt4 key购买 nike

我正在尝试编写一个模拟ATM的程序。用户可以使用个人识别码和帐号登录,检查其帐户余额并进行提款。我在初始化包含帐户信息的数组时遇到了麻烦,这是我到目前为止所拥有的:

#include <iostream>
#include <string>
using namespace std;

class Account
{
private: int accountNum;
string accountPin;
double balance;
void setPin();
void setAccountNum();

public: Account ()//default constructor
{
accountNum = -1;
accountPin = -1;
balance = 0.0;
};

Account (int accountNum, string accountPin, double balance)
//overloaded construtor
{
accountNum = accountNum;
accountPin = accountPin;
balance = balance;
};

void setAccountBalance(double bal);//acc balance setter

int getAccountNum() //acc balance getter
{
return balance;
}

bool confirmPin(string)//confirm pin# func
{

}
void updateBalance(double)


};

int main ()
{
int option;
//accounts array
Account account[]= (123, "abc123", 100.00), (456, "def456", 50.00),(789,"ghi789", 500.63);


//login code, unfinished
cout << "LOGIN\nEnter Account#: "<< endl;
cin >> accNum;
cout << "Enter pin#: "<<endl;;
getline(accPin);


//menu do while loop, unfinshed
do {

cout << "1. Check balance\n2.Make a deposit\n3.Logout\n";
cin >> option;

switch (option)
//check balance case
case 1:
// make a deposit case
case 2:

}
while (option !=3);
return 0;
}
第48行是需要初始化数组的位置,它包含帐号,pin码和帐户余额(按此顺序)。谁能指出我犯的错误?我在这里先向您的帮助表示感谢。

最佳答案

您需要在整个初始化程序列表以及每个元素的初始化程序周围加上花括号。

Account account[]= {
{123, "abc123", 100.00},
{456, "def456", 50.00},
{789,"ghi789", 500.63}
};

关于c++ - 初始化面向对象的数组C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64362720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com