我如何使用getline infile?

问题描述 投票:1回答:1

读取文件时,所有其他未以创建,存款,提取,余额开始的命令都将被跳过,并打印出错误消息。我正在尝试使用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;
}
c++
1个回答
2
投票

正如在对OP的注释中已经指出的那样,解决方案中存在多个编译和逻辑错误以及内存泄漏:

  1. [getline(inFile, junk);应更改为getline(statFile, junk);
  2. [else if (command != "Create", "Deposit", "Withdraw", "Balance")应替换为简单的else
  3. 导致NPE的所有{}语句缺少if-else:>
  4.             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。这是一种方法:
  2. int main() {
      ...
    
      for(Account* a : AccountArray) {
        delete a;
      }
    
      return 0;
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.