当我从 main 调用 setArray 并从 setArray 调用 prinArray 时,需要一个 ']' 和表达式
不太确定这里有什么问题。我必须读取 SaleSlip 值,然后找到每个销售人员每个 prodID 的总数。我这样做了,我知道这行得通。之后我必须输出一个二维数组,其中包含每个销售人员对每个 prodID 的销售额,这证明有点困难。我还没有完成对 setArray 的所有调用,但您可以了解要点。
对当前错误有任何帮助吗?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Create structure SaleSlip for each sale per product per employee
struct SaleSlip{
char name[20];
int prodID;
double value;
};
void setArray(string name, SaleSlip sales[]);
void printArray(string name, double product[][5], int j);
int main(){
// Create stream for text file
fstream slips;
// Initialize sales with 17 different members
SaleSlip sales[17];
// Open .txt for information reading
slips.open("SaleSlips.txt", ios::in);
if(slips.eof()){
cout << "Cannot open file(s) - SaleSlips.txt"<< endl;
exit(1);
}
int i = 0;
// Read and assign all names, product ids and prices to sales[]
while(!slips.eof()){
slips >> sales[i].name;
slips.ignore(80, ' ');
slips >> sales[i].prodID;
slips.ignore(80, ' ');
slips >> sales[i].value;
slips.ignore(80, '\n');
i++;
}
slips.close();
// Format for output
cout << " Prod1 Prod2 Prod3 Prod4 Prod5" << endl;
setArray("Bill", sales[]);
cout << endl << endl;system("pause");
return 0;
}
void setArray(string name, SaleSlip sales[]){
int j;
double product[4][5];
const char * namechar = name.c_str ();
if(strcmp (namechar, "Bill")) j = 0;
if(strcmp (namechar, "Eric")) j = 1;
if(strcmp (namechar, "Sookie")) j = 2;
if(strcmp (namechar, "Tara")) j = 3;
for(int i=0;i<17;i++){
if(strcmp (sales[i].name, namechar) == 0)
switch(sales[i].prodID){
case 1: product[j][1] += sales[i].value; break;
case 2: product[j][2] += sales[i].value; break;
case 3: product[j][3] += sales[i].value; break;
case 4: product[j][4] += sales[i].value; break;
case 5: product[j][5] += sales[i].value; break;
}
}
printArray(name, product[][5], j);
}
void printArray(string name, double product[][5], int j){
cout << name << ": ";
for(int i=0; i<5;i++)
cout << product[j][i] << " ";
cout << endl;
}
现在已格式化并正确输出:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Create structure SaleSlip for each sale per product per employee
struct SaleSlip{
char name[20];
int prodID;
double value;
};
void setArray(string name, SaleSlip sales[]);
void printArray(string name, double product[][5], int j);
int main(){
// Create stream for text file
fstream slips;
// Initialize sales with 17 different members
SaleSlip sales[17];
// Open .txt for information reading
slips.open("SaleSlips.txt", ios::in);
if(slips.eof()){
cout << "Cannot open file(s) - SaleSlips.txt"<< endl;
exit(1);
}
int i = 0;
// Read and assign all names, product ids and prices to sales[]
while(!slips.eof()){
slips >> sales[i].name;
slips.ignore(80, ' ');
slips >> sales[i].prodID;
slips.ignore(80, ' ');
slips >> sales[i].value;
slips.ignore(80, '\n');
i++;
}
slips.close();
// Format for output
cout << " Prod1 Prod2 Prod3 Prod4 Prod5" << endl;
setArray("Bill", sales);
setArray("Eric", sales);
setArray("Sookie", sales);
setArray("Tara", sales);
cout << endl << endl;system("pause");
return 0;
}
void setArray(string name, SaleSlip sales[]){
int j = -1;
double product[4][5] = {0};
const char * namechar = name.c_str ();
if(strcmp (namechar, "Bill") == 0) j = 0;
if(strcmp (namechar, "Eric") == 0) j = 1;
if(strcmp (namechar, "Sookie") == 0) j = 2;
if(strcmp (namechar, "Tara") == 0) j = 3;
for(int i=0;i<17;i++){
if(strcmp (sales[i].name, namechar) == 0)
switch(sales[i].prodID){
case 1: product[j][0] += sales[i].value;break;
case 2: product[j][1] += sales[i].value;break;
case 3: product[j][2] += sales[i].value;break;
case 4: product[j][3] += sales[i].value;break;
case 5: product[j][4] += sales[i].value;break;
default: cout << "Safety.";
}
}
printArray(name, product, j);
}
void printArray(string name, double product[][5], int j){
cout << setiosflags(ios::left | ios::fixed) << setprecision(2);
cout << setw(10) << name + ":";
for(int i=0; i<5;i++)
if(product[j][i] != 0)
cout << setw(10) << product[j][i];
else cout << setw(10) << 0;
cout << endl;
}