首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环读取dat文件不正确

循环读取dat文件不正确
EN

Stack Overflow用户
提问于 2013-02-17 09:43:36
回答 2查看 123关注 0票数 0

早上好,

我有一个读取dat文件的循环,但该循环读取的数据不正确。我的dat文件包含以下数据

代码语言:javascript
复制
46780976 3750.40
W 250.00
D 1200.00
W 75.00
W 375.00
D 1200.00
I 5.50
W 400.00
W 600.00
D 450.50
W 35.65

输出应为

代码语言:javascript
复制
Account number: 46780976
Opening balance: R3750.40

Transaction    Amount    Balance Bank costs
Withdrawal     250.00    4000.00
Deposit       1200.00Ct  2800.00
Withdrawal        75.00  2725.00
Withdrawal      1375.00   1350.00
Deposit        1200.00Ct 1550.00
Interest         5.50    1555.50
Withdrawal      400.00   1155.50
Withdrawal      600.00   555.50     25.00
Deposit         450.00Ct 1005.50
Withdrawal       35.65    969.85
Banking costs    25.00    969.60

Closing balance: R969.60

我得到了以下信息

代码语言:javascript
复制
Account number : 46780976

Opening Balance : R 3750.4

Transaction        Amount          Balance         Bank Costs


Withdrawal            250            3500.4
Deposit            0 Ct            3500.4
Interest              0            3500.4

Withdrawal            250            3225.4
Deposit            1200 Ct            4425.4
Interest              0            4425.4

Withdrawal            75            4325.4
Deposit            1200 Ct            5525.4
Interest              0            5525.4

Withdrawal            375            5125.4
Deposit            1200 Ct            6325.4
Interest              0            6325.4

Withdrawal            375            5925.4
Deposit            1200 Ct            7125.4
Interest              0            7125.4

Withdrawal            375            6725.4
Deposit            1200 Ct            7925.4
Interest              5.5            7930.9

Withdrawal            400            7505.9
Deposit            1200 Ct            8705.9
Interest              5.5            8711.4

Withdrawal            600            8086.4
Deposit            1200 Ct            9286.4
Interest              5.5            9291.9

Withdrawal            600            8666.9
Deposit            450.5 Ct            9117.4
Interest              5.5            9122.9

Withdrawal            35.65            9062.25
Deposit            450.5 Ct            9512.75
Interest              5.5            9518.25

Withdrawal            35.65            9457.6
Deposit            450.5 Ct            9908.1
Interest              5.5            9913.6
Bank Costs           25            9888.6            25


Closing Balance : R 9888.6
Press any key to continue . . .

我的代码:

代码语言:javascript
复制
do { 
   in_stream >> letter;

if (letter == 'W')

    in_stream >> withdrawal;
    openBalance -= withdrawal;
    cout << "\nWithdrawal" << "            "<< withdrawal << "            " << openBalance << endl;
    out_stream << withdrawal;

if (letter == 'D')

    in_stream >> deposit;
    openBalance += deposit;
    cout << "Deposit" <<"            "<< deposit <<" Ct" <<"            " << openBalance << endl;
    out_stream << deposit;


if (letter == 'I')

    in_stream >> interest;
    openBalance += interest;
    cout << "Interest" <<"              "<< interest <<"            " << openBalance << endl;
    out_stream << interest;

 if (openBalance < 1000)
     cout << "Bank Costs" <<"           "<< bankCost <<"            " << openBalance <<"            " << bankCost << endl;
    openBalance -= bankCost;
    out_stream << bankCost;

} while(!in_stream.eof());

请给我指出正确的方向

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-17 09:55:41

您的第一个问题是if代码块。在C++中,'if‘语句会立即影响后面的代码行或block.so...

代码语言:javascript
复制
if( Xxx )
    thisHappensOnlyIfXxxIsTrue
thisAlwaysHappens

if( Xxx )
    thisHappensOnlyIfXxxIsTrue
    thisAlwaysHappens  // even though it's indented


if( Xxx ) {
    thisHappensOnlyIfXxxIsTrue
    thisDependsOnXxxToo
}
thisAlwaysHappens

下一个问题是,您不是在每次循环重新启动时都清除变量。您需要插入一个

代码语言:javascript
复制
do {
   deposit = 0;
   interest = 0;
   withdrawal = 0;

在循环开始(或结束)时,确保不会重复使用上一次循环迭代中的值。

票数 2
EN

Stack Overflow用户

发布于 2013-02-17 09:53:31

因为第一行是:

代码语言:javascript
复制
46780976 3750.40

这一行将通过,但没有预期的内容:

代码语言:javascript
复制
in_stream >> letter;

当然,假设letter的类型是char

编辑

代码语言:javascript
复制
// You need to read in the initial values here

do { 
   in_stream >> letter;

if (letter == 'W')
{
    in_stream >> withdrawal;
    openBalance -= withdrawal;
    cout << "\nWithdrawal" << "            "<< withdrawal << "            " << openBalance << endl;
    out_stream << withdrawal;
}

if (letter == 'D')
{
    in_stream >> deposit;
    openBalance += deposit;
    cout << "Deposit" <<"            "<< deposit <<" Ct" <<"            " << openBalance << endl;
    out_stream << deposit;
}

if (letter == 'I')
{

    in_stream >> interest;
    openBalance += interest;
    cout << "Interest" <<"              "<< interest <<"            " << openBalance << endl;
    out_stream << interest;
 }

 if (openBalance < 1000)
 {
     cout << "Bank Costs" <<"           "<< bankCost <<"            " << openBalance <<"            " << bankCost << endl;
    openBalance -= bankCost;
    out_stream << bankCost;
  }

} while(!in_stream.eof());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14917181

复制
相关文章

相似问题

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