【问题标题】:C++ File I/O skipping line every new iteration, why?C++ 文件 I/O 每次新迭代都会跳行,为什么?
【发布时间】:2018-11-14 03:52:52
【问题描述】:

为什么程序会跳过每个新的 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;
}

我的逻辑有什么问题?

【问题讨论】:

  • 现在是您学习如何使用调试器的绝佳机会。您的调试器允许您一次运行一行程序;检查所有变量的值,因为它们发生变化;并观察你的程序的逻辑行为,以及为什么它会完全按照它的方式运行。了解如何有效地使用调试器是每个 C++ 开发人员必备的技能。所示代码中的逻辑缺陷似乎很简单,我敢肯定,一旦您使用调试器跟踪输入行的每一行是如何被读取的以及发生了什么,您很快就会弄明白全部。祝你好运。
  • 非常感谢@Sam Varshavchik 提到了 degugger,它非常有用,我不敢相信我的老师甚至在课堂上都没有提到它。

标签: c++ file-io


【解决方案1】:

你的逻辑有问题:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2017-09-11
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多