首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文本文件中的事务输出到屏幕

将文本文件中的事务输出到屏幕
EN

Stack Overflow用户
提问于 2014-01-19 17:17:27
回答 1查看 666关注 0票数 0

嗨,我正在处理一份工资申请,有四种选择,如下:

  1. 从商店银行帐户中减去一个金额(该帐户是文本文件" shop ")。您不需要将相同的金额添加到另一个帐户中,但是您应该在一个单独的文件中使用时间戳记录事务。应用程序应防止帐户余额透支。
  2. 列出屏幕上最近的五个事务。如果还没有五笔交易,那就把它们都列出来。
  3. 将帐户名称、号码和当前余额打印到屏幕上。
  4. 退出程序。

我已经完成了1,3和4,但我完全不知道如何去做第二名,我希望有人能给我指明正确的方向。

代码语言:javascript
复制
#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();
}
EN

回答 1

Stack Overflow用户

发布于 2014-01-19 18:36:02

您可以有一个交易列表,让我们说:

代码语言:javascript
复制
std::list<std::pair<int,std::string> > last_transactions

编写一个函数,在每次处理新事务(push_back)时,在列表末尾插入新的push_back,如果列表中已经有6项,它将删除最老的项(最前面的项-- pop_front)。而不能使用简单的for循环和迭代器来列出这些事务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21219953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档