难以读取和写入文件

时间:2014-11-30 03:51:36

标签: c++ while-loop fstream ifstream ofstream

好的,我有一个项目要完成。  IT基于银行系统UTT Banking Limited。每当银行开放给客户服务时,柜员都会得到他们的抽屉。 UTT Banking Limited每天有5名计票员,他们的名字是Darlene,Giselle,Chelsea,Samdaye和Natasha。每个柜员都有各自的期初余额,这些余额基于起始抽屉总额,记录在外部文件“dailyTransactions.txt”中。随着日期的进行,存款或取款的每笔交易都记录在外部文件中,“dailyTransactions.txt”记录为D 600.50或W 550.10。 外部文件“dailyTransactions.txt”在记录另一个出纳员之前拥有一个出纳员的所有交易。每个出纳员的录音结束用'E'表示。例如 达琳100.23 D 56.33 W 67.12 D 89.10 W 55.45 W 23.12 D 56.11 D 43.24 E Giselle 100.23 D 56.33 W 67.12 D 89.10 W 55.45 W 397.12 D 1089.10 W 550.45 W 9923.12 D 13500.11 D 45000.24 E 编写一个打开外部文件的“C ++”程序“dailyTransactions.txt”并读取数据并写入外部文件“dailyClosingBal.txt”以下信息:

Darlene         Opening Balance    100.23      The closing balance for 7 transactions:     199.32
Giselle          Opening Balance    100.23       The closing balance for 10 transactions:     48841.8

好的,所以这是我的代码。我是一个初学者,不知道我在做什么不幸但我不会放弃。当我运行代码时,它创建输出文件,但它仍然是空的:(我真的很感激任何建议

#include <fstream>
#include <iostream>
using namespace std;

int main ()

{
 ifstream inputfile;
 ofstream outputfile;

 string name;
 char trans;
 double op_bal, total = 0;
 string namesArr [5];
 double totalArr [5];
 int i=0;

 inputfile.open("dailyTransactions.txt");
 outputfile.open("dailyClosingBal.txt");



  while (!inputfile.eof() )

  {
    inputfile >> name;

    namesArr [i] = name;
    cout << namesArr[i] << " ";

    for (i=0; 1<5; i++)
    inputfile >> trans;

    while (trans != 'E')
    {
        if (trans == 'D'|| trans != 'W')
        {
            inputfile >> op_bal;
            total = total + op_bal;
        } 

        {
          if (trans == 'W'|| trans != 'D') 

            inputfile >> op_bal;
            total = op_bal - total; 

            inputfile.close();  
        } 

          outputfile << "Darlene Opening Balance:    100.23";                                               
          outputfile << "Giselle Opening Balance:    100.23";    
          outputfile.close();


    }
}   





system ("pause");
return 0;

}

这是正在阅读的文件的内容

Darlene 100.23 D 56.33 W 67.12 D 89.10 W 55.45 W 23.12 D 56.11  D 43.24 E 

Giselle 100.23  D 56.33 W 67.12 D 89.10 W 55.45 W 397.12 D 1089.10 
    W 550.45 W 9923.12 D 13500.11  D 45000.24 E
Chelsea 100.23 D 76.50 W 100.87 D 500.65 D 345.87 W 600.43 D 456.96 E
Samdaye 100.23 W 58.98 D 2500.00 W 456.87 W 345.65 D 456.89 W 43.76 D 235.87
    W 439.77 D 24.76 D 45.65 W 56.87 E
Natasha 100.23 D 568.00 W 78.00 D 56.00 W 350.00 D 10000.00 W 678.98 
    D 800.00 W 79.00 D 560.00 W 78.00 W 56.00 E 

0 个答案:

没有答案