Member Avatar for dmmckelv

I am back this week with another homework problem. My array is not working like I thought it would. My brain is about to explode trying to comprehend what is going on. This code will compile but, it won't do what I want it to. The array is supposed to have columns of employee numbers and rows of product numbers. Then you can input an employee number and product number and add to that total for a given number of days. For some reason my for statement for the number of days is not working. And the employee number and product numbers are not going where I think they should be. The total code at the bottom is a little sloppy and I can fix that once I can get the array problem figured out. Any help would be greatly appreciated.

Here is the code.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main ()
{
    int days = 0;
    int productTotal = 0;
    
    int sales[5][5] = {{0},{0}};
    int employeeNumber = 1;

    cout << "Enter number of days and program will request input for each day: ";
    cin >> days;

    cout << endl;

    for ( int x=0; x < days; x++)
    {
        while (employeeNumber > 0)
        {
            int productNumber = 1;
            cout << "Enter employee number(1-5) for day " << x << "(to move to next day enter 0): ";
            cin >> employeeNumber;

            cout << endl;

            switch (employeeNumber)
            {
            case 0:
                break;
            
            case 1:
                
                    cout << "Enter product number(1-5) (enter 0 to exit): ";
                    cin >> productNumber;
                    if (productNumber = 0)
                        break;
                    else
                    {
                    cout << endl;
                    cout << "Enter product total as a positive whole number: ";
                    cin >> productTotal;
                    cout << endl;
                    sales[0][productNumber-1] += productTotal;
                    cout << endl;
                    }
                break;
            
            case 2:
                while (productNumber > 0)
                {
                    cout << "Enter product number(1-5) (enter 0 to exit): ";
                    cin >> productNumber;
                    if (productNumber = 0)
                        break;
                    else
                    {
                    cout << endl;
                    cout << "Enter product total as a positive whole number: ";
                    cin >> productTotal;
                    cout << endl;
                    sales[1][productNumber-1] += productTotal;
                    cout << endl;
                    }
                }
                break;

            case 3:
                while (productNumber > 0)
                {
                    cout << "Enter product number(1-5) (enter 0 to exit): ";
                    cin >> productNumber;
                    if (productNumber = 0)
                        break;
                    else
                    {
                    cout << endl;
                    cout << "Enter product total as a positive whole number: ";
                    cin >> productTotal;
                    cout << endl;
                    sales[2][productNumber-1] += productTotal;
                    cout << endl;
                    }
                }
                break;

            case 4:
                while (productNumber > 0)
                {
                    cout << "Enter product number(1-5) (enter 0 to exit): ";
                    cin >> productNumber;
                    if (productNumber = 0)
                        break;
                    else
                    {
                    cout << endl;
                    cout << "Enter product total as a positive whole number: ";
                    cin >> productTotal;
                    cout << endl;
                    sales[3][productNumber-1] += productTotal;
                    cout << endl;
                    }
                }
                break;

            case 5:
                while (productNumber > 0)
                {
                    cout << "Enter product number(1-5) (enter 0 to exit): ";
                    cin >> productNumber;
                    if (productNumber = 0)
                        break;
                    else
                    {
                    cout << endl;
                    cout << "Enter product total as a positive whole number: ";
                    cin >> productTotal;
                    cout << endl;
                    sales[4][productNumber-1] += productTotal;
                    cout << endl;
                    }
                }
                break;
        }
    }
}
    
    cout << "The sales totals per employee per product number are as follows: " << endl;
    cout << "Employee#:   1 2 3 4 5" << endl;

    int i = 0;
    int j = 0;
    for ( i = 0; i < 5; i++ )
    {
        cout << "Product #: " << i+1 << " ";

        for (j = 0; j < 5; j++ )
        {
            cout << sales[i][j] << " ";
        }
        cout << endl;
    }


    for ( i = 0; i < 5; i++ )
    {
        int total = 0;
        for ( j = 0; j < 5; j++ )
        {
            total += sales[i][j];
        }
        cout << "Employee #" << i+1 << " month total: " << total << " ";
        cout << endl;
    }

    for ( j = 0; j < 5; i++ )
    {
        int total = 0;
        for (i = 0; j < 5; j++ )
        {
            total += sales[j][i];
        }
        cout << "Product #" << j+1 << " month total: " << total << " ";
        cout << endl;
    }
    }

In the statements "if(productnumber=0)" , i suppose you must have got a warning as i did. the test expression for the if statement is supposed to be a logical expression evaluating to true or false and not an assignment statement. i guess you meaned to write
"if(productnumber==0)" with 2 equal to signs.


sales[4][productNumber-1] += productTotal;

The above statement has been used once in each case of the switch statement with the switch variable "employeeNumber" and the only thing i noticed different in each case was in the idices of the 2d array sales. i guess this is what you must have meant

sales[employeeNumber-1][productNumber-1] += productTotal;

i didnt have time to go in to the logic of the program in too much detail but maybe sometimes correcting these if possible and reducing the number of switch statements to just a single line may help reduce the complexity and the size of the program to a large extent.

hope this solves your problem...:)

commented: He helped me! +1

The question I have is with your switch statement. What is the difference between all the cases? Could you describe the differences between each?

Member Avatar for dmmckelv

It was just an easy way for me to allow the user to select which employee sold a product.

well , you could have just said "employeenumber-1" in the first index of the array, as that is the only thing i can see as the difference in the diff cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.