将事务从文本文件输出到屏幕

时间:2014-01-19 17:17:28

标签: c++

您好我正在处理工资申请,其中有四个选项,如下所示:

  1. 从商店银行帐户中减去一笔金额(该帐户是文本文件“shop”)。您无需添加相同的金额 另一个帐户,但您应该在单独的文件中记录带有时间戳的事务。该 申请应防止帐户余额被透支。

  2. 列出屏幕上最近的五笔交易。如果还没有五笔交易 然后列出所有这些。

  3. 将帐户名称,号码和当前余额打印到屏幕上。

  4. 退出程序。

  5. 我已经完成了1,3和4但我完全不知道如何绕过2号。我希望有人能指出我正确的方向。

    #include <limits>
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <string>
    
    
    int  read_balance(void);
    void write_balance(int balance);
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int selection;
        int total;
        int attempts = 0;
        string name;
        string number;
    
    
    
        cout << "Correct login details entered!" << "" << endl;
        cout << "1. Transfer an amount" <<endl;
        cout << "2. List recent transactions"<<endl;
        cout << "3. Display account details and current balance"<<endl;
        cout << "4.Quit" << endl;
        cout << "Please enter menu number"<<endl;
        cin >> selection;
    
    
        switch(selection)
        {
        case 1: 
            cout << "How much do you wish to transfer?" << endl;
            int amount = 0;
    
            if (std::cin >> amount)
            {
                std::cout << "Transferred Amount:" << amount << "\n";
                int balance = read_balance();
    
                if (amount <= 0)
                {
                    std::cout << "Amount must be positive\n";
                }
                else if (balance < amount)
                {
                    std::cout << "Insufficient funds\n";
                }
                else
                {
                    int new_balance = balance - amount;
    
                    write_balance(new_balance);
                    std::cout << "New account balance: " << new_balance << std::endl;
    
                    fstream infile("time.txt", ios::app);
    
    
    
                    std::time_t result = std::time(nullptr);
                    std::string timeresult = std::ctime(&result);
    
    
                    infile << amount << std::endl;
                    infile << timeresult << std::endl;
                }
    
            }
        break;
    
    
        case 2:
            cout << "Here are you're recent transactions" <<endl;
        break;
    
        case 3:
            cout << "The account names is:" << name << endl;
            cout << "The account number is:" << number << endl;
            std::cout << "The current account balance is " << read_balance() << std::endl;
        break;
    
        case 4:
            system("pause");
            return 0;
    
        default:
            cout << "Ooops, invalid selection!" << endl;
            break;
    
        }
    
        system("pause");
        return 0;
    }
    
    
    int read_balance(void)
    {
        std::ifstream f;
        f.exceptions(std::ios::failbit | std::ios::badbit);
        f.open("shop.txt");
        int balance;
        f >> balance;
        f.close();
        return balance;
    }
    
    void write_balance(int balance)
    {
        std::ofstream f;
        f.exceptions(std::ios::failbit | std::ios::badbit);
        f.open("shop.txt");
        f << balance;
        f.close();
    }
    

1 个答案:

答案 0 :(得分:0)

你可以说一下交易清单:

std::list<std::pair<int,std::string> > last_transactions

编写一个函数,每次处理新事务(pair)时都会在列表的末尾插入新的push_back,如果列表中已有6个项目,它将删除最旧的一个(前面的那个 - pop_front)。您可以使用简单的for循环和迭代器来列出此事务。