C ++数组搜索和摘要

时间:2013-12-11 23:47:14

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

编辑:

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

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;
}

1 个答案:

答案 0 :(得分:0)

您需要一些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];
}

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