【发布时间】: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 >> data[empl][dps]; -
如果您认为
infile >> data[empl][dps];会从您的输入文件中填充整个二维数组,那您就错了。 -
另外,我没有看到
itemsOne、itemsTwo、itemsThree和itemsFive的任何初始化 -
啊,是的,这是一个愚蠢的错误。关于如何最好地初始化和放置我的数组的任何建议?是不是列和行大小有问题?
标签: c++ arrays search report file-io