C ++文件I / O每次新迭代都跳过一行,为什么?

时间:2018-11-14 03:52:53

标签: c++ file-io

为什么程序会跳过每个新的deptnum的第一行?

我正在尝试从如下文件中读取内容:

1 Suits   0300 100 092
1 Coats   0200 060 065
1 Shirts  1000 012 013
2 Dresses 0400 060 065
2 Coats   0185 184 200
2 Shoes   0600 040 030
3 Jeans   0200 040 035
3 Shoes   0200 030 034
4 Jeans   0300 042 043

deptnum是第一列。

当我写入另一个文件时,我得到:

                    Blinn Discount Apparel Company
                        Inventory Evaluation
                            10/12/2018

                     Unit Cost              Extended
      Quantity     Cost    Market       Cost     Market     Lower Cost
Mens Dept
 Suits         300   100.00     92.00     30000.00  27600.00
 Coats         200    60.00     65.00     12000.00  13000.00
 Shirts       1000    12.00     13.00     12000.00  13000.00
  Total                                  $54000.00 $53600.00    $53600.00
Womens Dept
 Coats         185   184.00    200.00     34040.00  37000.00
 Shoes         600    40.00     30.00     24000.00  18000.00
  Total                                  $112040.00 $108600.00    $108600.00
Girls Dept
 Shoes         200    30.00     34.00      6000.00   6800.00
  Total                                  $118040.00 $115400.00    $115400.00
Boys Dept
  Total                                  $118040.00 $115400.00    $115400.00
Total Inventory                                             $393000.00

它跳过了女子部门->连衣裙,女孩部门->牛仔裤和男孩部门->牛仔裤。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;
ofstream outFile;

int x = 1, deptnum, quant,cost,mkt,extcost,extmkt,totalcost = 0,totalmkt = 0,
lowcost,totalInv = 0;
char item [15];

inFile.open("blinn.dat");
outFile.open("blinn.dout");

if (!inFile)
    cout <<"\n\t\t Can't open data file: blinn.dat\n";


    else {
    outFile <<"\n\t                    Blinn Discount Apparel Company\n";
    outFile <<"\t                        Inventory Evaluation\n";
    outFile <<"\t                            10/12/2018\n";
    outFile <<"\n\t\t\t\t\t\t Unit Cost\t\t\t    Extended\n";
    outFile <<"\t\t  Quantity     Cost    Market       Cost     Market     Lower Cost";

    while (x < 5)
    {
        if (x == 1)
            outFile << "\nMens Dept";
        else if (x == 2)
            outFile << "\nWomens Dept";
        else if (x == 3)
            outFile << "\nGirls Dept";
        else if (x == 4)
            outFile << "\nBoys Dept";
        else
            break;

        while (inFile >> deptnum >> item >> quant >> cost >> mkt)
        {

            if (deptnum == x){

            extcost = quant * cost;
            extmkt = quant * mkt;

            outFile << left << "\n " << setw(7)<< item << "      "
                << right << setw(4)<< quant << "  "
                << right << setw(4) << cost << ".00    "
                << right << setw(3) << mkt << ".00     "
                << right << setw(5) << extcost<< ".00  "
                << right << setw(5) << extmkt << ".00";

                totalcost += extcost;
                totalmkt += extmkt;

                if (totalcost > totalmkt)
                    lowcost = totalmkt;
                else
                    lowcost = totalcost;


            }else
                break;
                }

            outFile << right << "\n  Total\t\t\t\t\t                 $" << totalcost << ".00 $"
                    << totalmkt << ".00    $"<< lowcost << ".00";

        x += 1;

            totalInv += lowcost;
    }
        }

outFile << "\nTotal Inventory\t\t\t\t\t\t                        $"<< totalInv<< ".00";


inFile.close ();
outFile.close ();

    return 0;
}

我的逻辑怎么了?

1 个答案:

答案 0 :(得分:0)

您的逻辑有问题:

if (deptnum == x) {
    // do something
}
else {
    break;
}

要实现else分支,您已经读取了一行deptnum != x(每个新deptnum的第一行),因此在下一个迭代器中,当前行被下一个输入行。