在读取文件时,需要跳过任何其他不是以 Create、Deposit、Withdraw、Balance 开头的命令,并打印出错误消息。我正在尝试使用 getline,但即使我已经进行了全面研究,我还是无法做到正确。我遇到的另一个问题是未读取 Balance 命令。我该如何解决这个问题?
需要读取文本文件
Create 1 1000.01
Create 2 2000.02
Create 3 3000.03
Deposit 1 11.11
Deposit 2 22.22
Withdraw 4 5000.00
Create 4 4000.04
Withdraw 1 0.10
Balance 2
Withdraw 2 0.20
Deposit 3 33.33
Withdraw 4 0.40
Bad Command 65
Balance 1
Balance 2
Balance 3
Balance 4
这是我的代码
#include <string>
#include <iostream>
#include "Account.h"
#include <fstream>
using namespace std;
int main() {
ifstream statFile;
int account;
double amount;
double balance;
string command;
string junk;
int id;
Account* AccountArray[10] = { nullptr };
Account* acc1;
statFile.open("bank.txt");
cout << "File opened." << endl;
while (statFile >> command) {
if (command == "Create")
{
statFile >> id >> amount;
acc1 = new Account(id, amount);
if (AccountArray[id] != nullptr)
cout << "That account id already exist." << endl;
else
AccountArray[id] = acc1;
cout << "Account number " << id << " created" << endl;
cout << "with an initial balance of " << amount << endl;
}
else if (command == "Deposit")
{
statFile >> id >> amount;
if (AccountArray[id] == nullptr)
cout << "That account id #" << id << "does not exist" << endl;
else
AccountArray[id]->deposit(amount);
cout << "Deposited " << amount << " into account #" << id << endl;
cout << "current balance is " << AccountArray[id]->getBalance() << endl;
}
else if (command == "Withdraw")
{
statFile >> id >> amount;
if (AccountArray[id] == nullptr)
cout << "That account id #" << id << " does not exist" << endl;
else
AccountArray[id]->withdraw(amount);
cout << "Withdrew " << amount << " from account #" << id << endl;
cout << "current balance is " << AccountArray[id]->getBalance() << endl;
}
else if (command == "Balance")
{
statFile >> id;
if (AccountArray[id] == nullptr)
cout << "That account id #" << id << " does not exist" << endl;
else
AccountArray[id]->getBalance();
cout << "Current balance in account #" << id << " is " << AccountArray[id]->getBalance() << endl;
}
else if (command != "Create", "Deposit", "Withdraw", "Balance")
{
getline(inFile, junk);
cout << "Unrecognized command" << endl;
}
}
return 0;
}
最佳答案
正如在对 OP 的评论中已经指出的那样,您的解决方案中存在多个编译和逻辑错误以及内存泄漏:
getline(inFile, junk);
应该改为getline(statFile, junk);
else if (command != "Create", "Deposit", "Withdraw", "Balance")
应该替换为简单的else
- 在导致 NPE 的所有
if-else
语句中缺少{}
:
if (AccountArray[id] == nullptr)
cout << "That account id #" << id << " does not exist" << endl;
else
AccountArray[id]->withdraw(amount);
cout << "Withdrew " << amount << " from account #" << id << endl;
// Following line gets executed even for AccountArray[id] == nullptr
cout << "current balance is " << AccountArray[id]->getBalance() << endl;
// Fixed code:
if (AccountArray[id] == nullptr)
{
cout << "That account id #" << id << " does not exist" << endl;
}
else
{
AccountArray[id]->withdraw(amount);
cout << "Withdrew " << amount << " from account #" << id << endl;
cout << "current balance is " << AccountArray[id]->getBalance() << endl;
}
- 为
Accounts
分配的内存未释放。对于程序中的每个new
,您都应该有一个delete
。这是一种方法:
int main() {
...
for(Account* a : AccountArray) {
delete a;
}
return 0;
}
关于c++ - 我如何使用 getline infile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760282/