C++ Array Search and Summary

EDIT:

I have made the following changes to my code:

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

I have manually checked the logic with an array put in the old fashion way and am getting zero values for itemsFour, itemsFive, and bizarrely the sales data from the 500 sub-array is not going through.

So I have been working on a project and have hit a bit of a stumbling block. My goal is to take the following array from a the following text file:

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

And find the items sold by each employee, the total items sold, the total sales, and the total tax.

Here is the code that outputs nothing but a series of zeros:

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

Answers


You need some for-loops to populate your data array. Something like this:

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

Note that if your data file has headers on it as your example does above, you'll need to read and discard those before this loop. Something like string temp; std::getline(cin, temp); would do.

In the loops that compute items, taxes and money, you have row and column switched, I think. If your first index is empl, which goes 0..4, then you ought to switch these like so:

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

I believe you need to make the same switch in the big loop that follows these as well.

And finally, in these statements, you need to use ==, not = to compare values:

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

I haven't vetted the logic of your code to great detail, but if you fix these items, you'll be in better shape and can get to the next steps.


Need Your Help

Facebook - get friends of friends

php facebook facebook-graph-api

I'm trying to do a script which takes the logged-in user - retrieves the list of friends and loops through these friends to get their friendlist. Meaning the users friends-of-friends. I've read som...

About Security of machinekey

asp.net web-config forms-authentication machinekey machine-config

We have a scenario using asp.net Forms Authentication in a web farm and need to setup identical &lt;machinekey /&gt; sections on each servers .config file.