【问题标题】:C++ Array Search and SummaryC++ 数组搜索和总结
【发布时间】:2013-12-11 23:47:14
【问题描述】:

编辑:

我对我的代码进行了以下更改:

ifstream infile;

infile.open("sales.txt");

if ( !infile.is_open() ) cerr << "file didn't open" << endl;

if (!infile)
{
    cout << "Forget something?"
        << endl;
    return 1;
}

for(int i = 0; i < empl; i++)
    for(int j = 0; j < dps;  j++)
        cin >> data[i][j];

items = 0;
    for (row = 0; row < empl; row++)
    items = items + data[row][item];

money = 0;
    for (row = 0; row < dps; row++)
    money = money + data[row][sale];
taxes = 0;
    for (row = 0; row < empl; row++)
        taxes = taxes + data[row][tax];



itemsOne = 0;
itemsTwo = 0;
itemsThree = 0;
itemsFour = 0;
itemsFive = 0;

for(row = 0; row < dps; row++)
{
    if(idOne == data[row][ID])
    {
        itemsOne += data[row][item];
    }
    if(idTwo == data[row][ID])
    {
        itemsTwo += data[row][item];
    }
    if(idThree == data[row][ID])
    {
        itemsThree += data[row][item];
    }
    if(idFour == data[row][ID])
    {
        itemsFour += data[row][item];
    }
    if(idFive == data[row][ID])
    {
        itemsFive += data[row][item];
    }
}



cout << "***********************************" << endl;
cout << "Employee 100 sold : " << itemsOne << endl;
cout << "Employee 200 sold : " << itemsTwo << endl;
cout << "Employee 300 sold : " << itemsThree << endl;
cout << "Employee 400 sold : " << itemsFour << endl;
cout << "Employee 500 sold : " << itemsFive << endl;
cout << "***********************************" << endl;
cout << "Total items sold : " << items << endl;
cout << "Total revenue :" << revenue << endl;
cout << "Sales Tax: " << taxes << endl;
cout << "Gross Income: " << money << endl;
cout << "***********************************" << endl;
cout << "\n" << endl;
cout << "***********************************" << endl;
cout << "so long and thanks for all the fish" << endl;
cout << "***********************************" << endl;

system("pause");
return 0;   


system("pause");
return 0;
}

我已经手动检查了一个以旧方式放置的数组的逻辑,并且得到了 itemsFour、itemsFive 的零值,奇怪的是来自 500 子数组的销售数据没有通过。

所以我一直在做一个项目,遇到了一个绊脚石。我的目标是从以下文本文件中获取以下数组:

ID  Items Sales Tax
100 23    1200   110
200 12    7500   120
100 10    1000   230
300 11    1100   110
500 47    5800   122

并找出每个员工销售的物品、销售的总物品、总销售额和总税收。

这是只输出一系列零的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <array>
#include <iomanip>
#include <cmath>

using namespace std;


const int empl = 5;
const int dps = 4;
float data [empl][dps];
int row;
int col;
double itemsOne, itemsTwo, itemsThree, itemsFour, itemsFive, items, revenue, taxes, money;
int item = 1, sale = 2, tax = 3;
float idOne = 100, idTwo = 200, idThree = 300, idFour = 400, idFive = 500;


int main()
{
ifstream infile;

infile.open("sales.txt");

infile >> data[empl][dps];


items = 0;
    for (col = 0; col < dps; col++)
    items = items + data[item][col];

taxes = 0;
    for (col = 0; col < dps; col++)
        taxes = taxes + data[tax][col];

money = 0;
        for (col = 0; col < dps; col++)
        money = money + data[sale][col];

itemsFour = 0;
for(col = 0; col < dps; col++)
{
    if(idOne = data[0][col])
    {
        itemsOne += data[1][col];
    }

    if(idTwo = data[0][col])
    {
        itemsTwo += data[1][col];
    }

    if(idThree = data[0][col])
    {
        itemsThree += data[1][col];
    }


    if(idFour = data [0][col])
    {
        itemsFour = itemsFour + data[1][col];
    }

    if(idFive = data[0][col])
    {
        itemsFive += data[1][col];
    }
}


revenue = money + taxes;


cout << "***********************************" << endl;      //Step 5 outputs report to command line
cout << "Employee 100 sold : " << itemsOne << endl;
cout << "Employee 200 sold : " << itemsTwo << endl;
cout << "Employee 300 sold : " << itemsThree << endl;
cout << "Employee 400 sold : " << itemsFour << endl;
cout << "Employee 500 sold : " << itemsFive << endl;
cout << "***********************************" << endl;
cout << "Total items sold : " << items << endl;
cout << "Total revenue :" << revenue << endl;
cout << "Sales Tax: " << taxes << endl;
cout << "Gross Income: " << money << endl;
cout << "***********************************" << endl;
cout << "\n" << endl;
cout << "***********************************" << endl;
cout << "so long and thanks for all the fish" << endl;
cout << "***********************************" << endl;

system("pause");
return 0;   


system("pause");
return 0;
}

【问题讨论】:

  • 我不确定您的其余代码,但这是一个超出范围的数组访问:infile &gt;&gt; data[empl][dps];
  • 如果您认为 infile &gt;&gt; data[empl][dps]; 会从您的输入文件中填充整个二维数组,那您就错了。
  • 另外,我没有看到 itemsOneitemsTwoitemsThreeitemsFive 的任何初始化
  • 啊,是的,这是一个愚蠢的错误。关于如何最好地初始化和放置我的数组的任何建议?是不是列和行大小有问题?

标签: c++ arrays search report file-io


【解决方案1】:

您需要一些 for 循环来填充您的 data 数组。像这样的:

for (int i = 0; i < empl; i++)
    for (int j = 0; j < dps; j++)
        cin >> data[i][j];

请注意,如果您的数据文件像上面的示例一样包含标题,则需要在此循环之前读取并丢弃那些。像string temp; std::getline(cin, temp); 这样的东西就可以了。

我认为,在计算 itemstaxesmoney 的循环中,您已经切换了行和列。如果你的第一个索引是 empl,它是 0..4,那么你应该像这样切换它们:

items = 0;
for (row = 0; row < empl; row++)
    items = items + data[row][item];

我相信您也需要在后面的大循环中进行相同的切换。

最后,在这些语句中,您需要使用==,而不是= 来比较值:

if(idOne == data[0][col])  // use == here, not =
{
    itemsOne += data[1][col];
}

我没有详细审查您的代码逻辑,但如果您修复这些项目,您的状态会更好,可以进入下一步。

【讨论】:

  • 好的,所以我已经更改了您指出的逻辑,但我仍在努力将文本文件数据放入我的数组中。我目前正在使用此代码:int i = 0; int a, b, c, d; for(int i = 0; i &lt; empl; i++) while(i &lt; empl &amp;&amp; infile &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d) { data[i][dps] = a; data[i][dps] = b; data[i][dps] = c; data[i][dps] = d; ++i; } 但我仍然面临全零的问题。
  • @user3093333 :请尝试使用我在上面答案顶部写的循环。您在该评论中编写的循环将不起作用。一方面,它使用dps 作为数组索引,但它不是有效的数组索引。
  • 所以我使用了上面的 for 循环,但它仍然填充全零。
  • @user3093333:你确定它用全零填充data,还是你的其余逻辑产生全零?你能在调试器中单步执行并在其中查看data 吗?另外,你确定infile.open("sales.txt"); 成功了吗?你可以通过if ( !infile.is_open() ) cerr &lt;&lt; "file didn't open" &lt;&lt; endl;查看它
  • 所以我对原始代码进行了编辑,并解释了新代码下面的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
  • 2013-04-30
  • 2020-04-28
  • 1970-01-01
  • 2018-10-25
  • 2019-10-05
相关资源
最近更新 更多