c++ - 我如何使用 getline infile?

标签 c++

在读取文件时,需要跳过任何其他不是以 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 的评论中已经指出的那样,您的解决方案中存在多个编译和逻辑错误以及内存泄漏:

  1. getline(inFile, junk); 应该改为 getline(statFile, junk);
  2. else if (command != "Create", "Deposit", "Withdraw", "Balance") 应该替换为简单的 else
  3. 在导致 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;
            }
  1. Accounts 分配的内存未释放。对于程序中的每个 new,您都应该有一个 delete。这是一种方法:
int main() {
  ...

  for(Account* a : AccountArray) {
    delete a;
  }

  return 0;
}

关于c++ - 我如何使用 getline infile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760282/

相关文章:

c++ - 了解 C++ 中子类的类

c++ - 是否可以为 boost::random::uniform_int_distribution<> 设置确定性种子?

c++ - 访问存储在 vector 中的指针内的字符串时崩溃

c++ - 将 'using' 指令与部分重载 : gcc feature or intel bug? 相结合

c++ - 包含 header 会导致解析问题

c++ - 如果未提供模板参数,如何禁用模板?

c++ - 打印半金字塔

c++ - 如何使用CreateDIBSection在BITMAPINFO中写入颜色数据?

c++ - 在OpenCV C++中播放视频文件

c++ - 模板作为类中的参数