2014-01-19 35 views
0

嗨,我上有四個選項的工資應用程序的工作1.4.3事務的情況如下:從文本文件到屏幕

  1. 減去量從商店的銀行賬戶(賬號爲文本文件「店」)。您不需要在 另一個帳戶中添加相同的金額,但您應該使用時間戳將交易記錄在單獨的文件中。 應用程序應該防止帳戶餘額透支。

  2. 列出五個最近的交易到屏幕上。如果還沒有五筆交易 則列出所有這些交易。

  3. 將帳戶名稱,編號和當前餘額輸出到屏幕上。

  4. 退出程序。

我已完成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(); 
} 
+0

提示 - 保留最近5次交易的[循環緩衝區](http://en.wikipedia.org/wiki/Circular_buffer)。 – Dukeling

回答

0

你可以有交易的列表,可以說:

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

編寫將在列表的每次處理新的事務(push_back)時間的末尾插入新pair的功能,如果列表中已有6個項目,它將刪除最舊的一個(前面的那個 - pop_front)。你可以使用簡單的for循環和迭代器來列出這些事務。

+0

認爲對我來說這是一個先進的讓我在這個階段得到我的頭,但無論如何謝謝 – user3057816

+0

@ user3057816所以你現在熟悉什麼?類/結構,表? – prajmus

+0

以及我目前正在學習課程/結構,但肯定不會說我對他們很滿意。我認爲這部分任務可能只是我的一小部分 – user3057816