简体   繁体   中英

Visual C++ - Read text file, output 2D array

Expects a ']' and expression when i call setArray from main and prinArray from setArray

Not really sure what is wrong here. I have to read in the SaleSlip values, then find the total per prodID per salesperson. 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. I haven't finished all the calls to setArray, but you can get the gist.

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;
}
  1. You should check if strcmp returned 0 if the strings are equal:

     if (strcmp(namechar, "Bill") == 0); 
  2. The [] isn't needed when passing the array as a parameter, only when you are creating one:

     setArray("Bill", sales); // ... printArray(name, product, j); 
  3. The first iteration of the for loop causes Undefined Behavior in your program because the product array is uninitialized. Use a std::vector instead.

Your should probably be using a std::map<std::string, double> instead of an array of double

change

setArray("Bill", sales[]);

to

setArray("Bill", sales);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM