Please help with c++ program?

This program is supposed to use a double scripted array-A company has 4 salespeople(numbered 1-4)who each sell five different products(numbered 1-5)Once a day, each salesperson passes in a slip for each separate product sold. In the slip, the information is as follows: a. The salesperson number
b. The product number.
c. The total dollar value of the product sold that day
i tried a fresh start, but a couple statements i cannot figure out. Could you help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  
//defining libraries to be used
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
const int PEOPLE = 5;
const int PRODUCTS = 6;
int sales[6][7];
double value;
double totalSales;
double productSales[ PRODUCTS ] = { 0.0 };
int salesPerson;
int product;

// enter sales slips
cout << "Enter the salesperson (1 - 4), product number (1 - 5), and " << "total sales.\nEnter -1 for the salesperson to end input.\n";

cin >> salesPerson;

// continue receiving input for each salesperson until -1 is entered
while ( salesPerson != -1 )
{
cin >> product >> value;
/* Could you write a statement that adds values to the proper element in the sales array */
cin >> salesPerson;
} // end while

cout << "\nThe total sales for each salesperson are displayed at the "
<< "end of each row,\n" << "and the total sales for each product "
<< "are displayed at the bottom of each column.\n " << setw( 12 )
<< 1 << setw( 12 ) << 2 << setw( 12 ) << 3 << setw( 12 ) << 4
<< setw( 12 ) << 5 << setw( 13 ) << "Total\n" << fixed << showpoint;

// display salespeople and sales
for ( int i = 1; /* Im not sure what condition goes here */; i++ )
{
totalSales = 0.0;
cout << i;

// add total sales, and display individual sales
for ( int j = 1; /* Im not sure what condition goes here */; j++ )
{
/*Which statement would add the current sales element to totalSales */
cout << setw( 12 ) << setprecision( 2 ) << sales[ i ][ j ];
/* Which statement would add the current sales element to productSales */
} // end inner for

cout << setw( 12 ) << setprecision( 2 ) << totalSales << '\n';
} // end outer for

cout << "\nTotal" << setw( 8 ) << setprecision( 2 ) << productSales[ 1 ];

// display total product sales
for ( int j = 2; j < PRODUCTS; j++ )
cout << setw( 12 ) << setprecision( 2 ) << productSales[ j ];

cout << endl;
} // end main
Hello its20alif,

You ask
/* Could you write a statement that adds values to the proper element in the sales array */
Yes it could be done, but it would not work very well because the "sales array" is not correct.

First you need to determine what information needs to be stored in the "sales array" and how you are going to work with it. As I see it you have four sales people, so there is your first dimension. But if you make the first dimension five instead of four and do not use row zero you could use the "salesPerson"number of 1 - 4 to access the first dimension. The second dimension is a little more involved because you need to keep track of two pieces of information the product number and the total dollar value sold that day. Keeping that in mind you would have five groups of two, so your array size would be ten or eleven if you do not use element zero.

Then in the while loop on on line 25 the "cin" would look something like this:

cin >> sales[salesperson][col] >> salesPerson[salesPerson][col + 1].

This way you will store the information directly into the array and the value of the variable "col" will be determined be a series of "if" statements allowing each product/amount sold to be in the same place in the array.

I have thought about this for awhile, but have not tried any code yet. I will work on this in and have something in a couple of hours.

Hope that helps,

Andy
Hello its20alif,

This is a thought I had. May not be the best way, but it works. Read the comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const int PEOPLE = 5;
const int PRODUCTS = 6;
// You should always initialize your variables.
double sales[5][11]{ 0.0 };  // <--- Changed size of array and set entire array to zero. Also Changed to double. 1st dimension could also be "PEOPLE".
double product{ 0.0 }, value{ 0.0 };
double totalSales{ 0.0 };
double productSales[PRODUCTS] = { 0.0 };
int salesPerson{ 0 };
int col{ 1 };  // <--- Added for use in while loop. needs to start at 1.

// enter sales slips
std::cout << " Enter the salesperson (1 - 4), product number (1 - 5), and " << "total sales.\n Enter -1 for the salesperson to end input.\n Sales Person: ";

std::cin >> salesPerson;
//  <--- A good place to check that the value of salesPerson is > 0 and < 5.

// continue receiving input for each salesperson until -1 is entered
while (salesPerson != -1)
{
	std::cout << "\n Enter product: ";  //<--- Split product and value to separate inputs.
	std::cin >> product; // <--- product needed for if statements.

	// <--- A good place to check that the value of product is > 0 and < 6.

	if (product == 1) col = 1;  // <--- if statements to determine col number.
	if (product == 2) col = 3;  //  col number is the stare of the product value pair. 
	if (product == 3) col = 5;
	if (product == 4) col = 7;
	if (product == 5) col = 9;

	sales[salesPerson][col] = product;  // <--- Stores product number based on "salesPerson" and "col" number.
	                                    //  This keeps the product in the same place in the array for later processing.
	std::cout << "\n Enter amount: ";
	std::cin >> value;

	// Used to determine if value needs to be added to or just stored. Thinking ahead, may not be needed.
	if (sales[salesPerson][col + 1] > 0)

	// Add new value to array.
	sales[salesPerson][col + 1] = value;  // <--- Stores value number based on "salesPerson" and "col" number.

	std::cout << "\n Enter Sales Person. -1 to quit: ";
	std::cin >> salesPerson;

} // end while

for (int i = 1; /* I am not sure what condition goes here */; i++) // <--- Center part is based on the size of
{                                                                //  each dimension. Same for other loop.
	totalSales = 0.0;                                            //  i and j would change to i+=2 and j+=2.
	std::cout << i;

	// add total sales, and display individual sales
	for (int j = 1; /* I am not sure what condition goes here */; j++)
	{
		// your code here.
		//  Knowing how the sales array is set up you can step through the array and possibly use the
		//  productSales array to store the totals for each product. Have not worked it out that far yet.
		//  The produceSales array may work better a 2D array with the first dimension for each sales person
		//  and the second dimension for total sold.
	}
}


I have worked on the input of the program, but have not worked on the rest yet. The output to the display should not be hard to figure out.

Hope that helps,

Andy
Thank you so much, i understand what you said in the comments, thanks for clearing the confusion i had. btw on the other post, the guy was not responding. Thank you again
the guy was not responding.

Unbelievable, uh?
Topic archived. No new replies allowed.