[英]Visual C++ - Read text file, output 2D array
Expects a ']' and expression when i call setArray from main and prinArray from setArray 当我从main调用setArray和从setArray调用prinArray时,期待一个']'和表达式
Not really sure what is wrong here. 不确定这里有什么问题。 I have to read in the SaleSlip values, then find the total per prodID per salesperson. 我必须读取SaleSlip值,然后找出每个销售人员的每个prodID总数。 I did so and I know that works. 我这样做了,我知道这很有效。 After that I have to output a 2D array that has each salesperson's sales of each prodID, and that is proving a little difficult. 之后我必须输出一个2D数组,其中包含每个prodID的每个销售人员的销售额,这证明有点困难。 I haven't finished all the calls to setArray, but you can get the gist. 我还没有完成对setArray的所有调用,但是你可以获得要点。
Any help with the current errors? 有关当前错误的任何帮助吗?
#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;
}
This is now formatted and properly outputted: 现在格式化并正确输出:
#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;
}
You should check if strcmp
returned 0 if the strings are equal: 如果字符串相等,您应该检查strcmp
返回0:
if (strcmp(namechar, "Bill") == 0);
The []
isn't needed when passing the array as a parameter, only when you are creating one: 将数组作为参数传递时不需要[]
,只有在创建一个时:
setArray("Bill", sales); // ... printArray(name, product, j);
The first iteration of the for loop causes Undefined Behavior in your program because the product
array is uninitialized. for循环的第一次迭代会在程序中导致Undefined Behavior,因为product
数组未初始化。 Use a std::vector
instead. 请改用std::vector
。
Your should probably be using a std::map<std::string, double>
instead of an array of double 您应该使用std::map<std::string, double>
而不是double数组
change 更改
setArray("Bill", sales[]);
to 至
setArray("Bill", sales);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.