期待一个']'和表达式
不确定这里有什么问题。我必须读取SaleSlip值,然后找出每个销售人员的每个prodID总数。我这样做了,我知道这很有效。之后我必须输出一个2D数组,其中包含每个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;
}
答案 0 :(得分:2)
如果字符串相等,您应该检查strcmp
是否返回0:
if (strcmp(namechar, "Bill") == 0);
将数组作为参数传递时不需要[]
,只有在创建数组时才需要setArray("Bill", sales);
// ...
printArray(name, product, j);
:
product
for循环的第一次迭代会导致程序中出现未定义的行为,因为std::vector
数组未初始化。请改用std::map<std::string, double>
。
您应该使用{{1}}而不是双数组
答案 1 :(得分:0)
变化
setArray("Bill", sales[]);
到
setArray("Bill", sales);