Programming Help for a 3 dimensional Array program.

Dear coders, I need your help. I am supposed to:
Use a 3d Array [Stores 0-1] [Months 0-11] [Departments 0-1]. Initialize array with 48 sale val. (doubles), ask user to enter a month, output 4 values for actual sales for that month for (Store0, Store1, Dept0, Dept1) and output calculated Total Sales values for (Store0(Dept1&2), Store1(Dept1&2), Dept1(in Both Stores), Dept2(in Both Stores). Then I am supposed to begin reading sets of 48 values separated by -1 from a file into the array, and after the array is populated with each set, prompt user to input month and display the above actual sales and total sales again. Ask the user for input, until the end of file is reached. My program works for the initialized array (the values that I initialize it with), but then doesn't ask the user for input and does not read from file/continue outputting. Please

void arrayPrint(float Sale[NUM_STORES][NUM_MONTHS][NUM_DEPTS], int depth, int length, int width)
{

for (int plane = 0; plane < NUM_STORES; plane++) {

    for (int row = 0; row < NUM_MONTHS; row++)
    {

        for (int col = 0; col < NUM_DEPTS; col++)
        {
            cout << Sale[plane][row][col] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

}

int returnMonth()
{
int Month = -1;

do {

    cout << "Please enter a value 0-11 for a Month: " << endl;

    cin >> Month;
    if (Month < 0 || Month > 11)
        cout << "Sorry, you have entered the wrong value! Please Try Again!" << endl;

}

while ((Month < 0) || (Month > 11));
return Month;

}
void printSales(float Sales[NUM_STORES][NUM_MONTHS][NUM_DEPTS], int month, int stores, int months, int depts)
{
double sumStore0 = Sales[0][month][0] + Sales[0][month][1];
double sumStore1 = Sales[1][month][0] + Sales[1][month][1];
double sumDept0 = Sales[0][month][0] + Sales[1][month][0];
double sumDept1 = Sales[0][month][1] + Sales[1][month][1];

cout << "                      Total Store Sales" << endl;
cout << "            " << Sales[0][month][0] << " " << Sales[0][month][1] << " Store 1: " << sumStore0 << endl;
cout << "            " << Sales[1][month][0] << " " << Sales[1][month][1] << " Store 2: " << sumStore1 << endl;
cout << "            " << Sales[0][month][0] << " " << Sales[1][month][0] << " " << endl;
cout << "            " << Sales[0][month][1] << " " << Sales[1][month][1] << " " << endl;
cout << "Tot Dept. Sales: Dept1: " << sumDept0 << ". Dept 2: " << sumDept1 << " " << endl;

}

int main()
{
//int A[MAX_STORES][MAX_MONTHS][MAX_DEPTS] = { { 3,5 },{ 4,5 },{ 2,2 } }; //two dimensional array with 6 elements initialized

float Sale[NUM_STORES][NUM_MONTHS][NUM_DEPTS] =
{ 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,
    2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2,
    3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2,
    2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2
};

int sep = -1;


ifstream inFile;
inFile.open("intInputSales.txt");
//Check For Error
if (inFile.fail())
{
    cout << "Input file not found!" << endl;
    exit(1);
}
int positiveInteger1 = 0;
int error = 0;
int month = -1;
while (!inFile.eof())
{
    if (error == 1)
    {
        cout << "invalid data set" << endl;
    }
    else
    {
        arrayPrint(Sale, NUM_STORES, NUM_MONTHS, NUM_DEPTS);

        month = returnMonth();

        printSales(Sale, month, NUM_STORES, NUM_MONTHS, NUM_DEPTS);
    }

    inFile >> positiveInteger1;
    cout << " " << positiveInteger1 << endl;
    int count = 0;
    error = 0;
    while (((positiveInteger1 != sep) && (positiveInteger1 > 0)) && (count != 48) && !inFile.eof())
    {
        for (int plane = 0; plane < NUM_STORES; plane++)
        {
            for (int row = 0; row < NUM_MONTHS; row++)
            {
                cout << "row " << row;

                for (int col = 0; col < NUM_DEPTS; col++)
                {
                    cout << "col " << col << endl;

                    inFile >> positiveInteger1;
                    Sale[plane][row][col] = positiveInteger1;

                    //cout << row << " " << col << " " << positiveInteger1 << endl;
                    cout << positiveInteger1 << endl;
                    count++;
                        printSales(Sale, month, NUM_STORES, NUM_MONTHS, NUM_DEPTS);
                        returnMonth();
                    if (positiveInteger1 == -1 && !(plane == NUM_STORES && row == NUM_MONTHS - 1 && col == NUM_DEPTS - 1))
                    {
                        cout << "not enough data" << endl;
                        error = 1;
                        break;
                    }
                }
                returnMonth();
                if (error == 1) break;
            }
            if (error == 1) break;
        }
    }
    if (positiveInteger1 != -1)
    {
        error = 1;
        while (positiveInteger1 != -1) inFile >> positiveInteger1;
        cout << "too many" << endl;
    }


}

return 0;

}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories