0

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;
}

2 Answers 2

2
  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

Sign up to request clarification or add additional context in comments.

11 Comments

If I do #2 I get this: error C2664: 'printArray' : cannot convert parameter 2 from 'double [4][5]' to 'double' same with setArray
Is product an array or a double variable.
should be a 2d array, the reason i had double was for sales[].value.
The signatures of your functions should not be changed. I only said to change the way you were calling the function.
Ahhaa....I feel stupid now. I tested it, I am getting the same large negative values for each product, the strcmp condition is working. I put a default case to test the switch, and it doesn't use it.
|
0

change

setArray("Bill", sales[]);

to

setArray("Bill", sales);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.