Storing user input into 2-dimensional array?

Hi all, having some trouble with storing user input into this particular 2-dimensional array. I have to get the sales per car type per color type, and print the sum of sales of the car types AND color types separately. Here's what I have so far. Should I declare variables for each type of car/color that the user inputs?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int CAR_TYPES = 2;
const int COLOR_TYPES = 3;
double sales[CAR_TYPES][COLOR_TYPES];

int main(){

	for (int i = 0; i<2; i++){}
	//Ask the user for car type sales
	cout << "Enter 2 car type sales: " << endl;
	cin >> sales[i][0];

	//Ask the user for amount of sales per color
	cout << "Enter 3 color type sales: " << endl;
	cin >>sales

	system("pause");
	return 0;
}
Line 11: The {} at the end of the line terminates the for statement. Therefore your for loop does not do anything.

Line 16: Since you've terminated the for statement, i has gone out of scope and is not valid here. Since the second subscript is 0, you saying that this is the sales for color 0.

Line 20: You need two subscripts here.

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int CAR_TYPES = 2;
const int COLOR_TYPES = 3;
const char * car_names[] = 
    {   "Ford",
        "Chevy",
    };
const char * color_names[] = 
    {   "Red",
        "Blue",
        "Green",
    };

double sales[CAR_TYPES][COLOR_TYPES];

int main()
{   for (int i = 0; i<CAR_TYPES; i++)
    {   for (int j=0; j<COLOR_TYPES; j++)
        {   cout << "How many " << color_names[j] << " " << car_names[i] << "s were sold";
            cin >> sales[i][j];
        }
    }
    // Up to you to display totals by car type and color
    system("pause");
    return 0;
}

Last edited on
Is it something like this?

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
int main(){

	int totalfords = 0;
	int totalchevy = 0;
	int totalreds = 0;
	int totalblues = 0;
	int totalgreens = 0;

	//for loop to ask the user for the number of cars sold.
	for (int i = 0; i<CAR_TYPES; i++)
	{
		for (int j = 0; j<COLOR_TYPES; j++)
		{
			cout << "How many " << colors[j] << " " << carbrands[i] << "s were sold?" << endl;
			cin >> sales[i][j];
			
			if ((carbrands[0] && colors[0]) > 0) {
				totalfords++;
				totalreds++;
			}

			if (carbrands[0] && colors[1] > 0){
				totalfords++;
				totalblues++;
			}
			
			if (carbrands[0] && colors[2] > 0){
				totalfords++;
				totalgreens++;
			}

			if (carbrands[1] && colors[0] > 0){
				totalchevy++;
				totalreds++;
			}

			if (carbrands[1] && colors[1] > 0){
				totalchevy++;
				totalblues++;
			}

			if (carbrands[1] && colors[2] > 0){
				totalchevy++;
				totalreds++;
			}
		}
	}
	//print the total by car type
	cout << "Total Fords sold: " << totalfords << endl;
	cout << "Total Chevies sold: " << totalchevy << endl;

	//print the total by car color
	cout << "Total red cars sold: " << totalreds << endl;
	cout << "Total blue cars sold: " << totalblues << endl;
	cout << "Total green cars sold: " << totalgreens << endl;
Actually, I have this now, but the output still isn't correct. It seems to only be adding input to the total number of fords and the total number of red cars, but the numbers are too high.

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
62
63
64
int main(){

	int totalfords = 0;
	int totalchevy = 0;
	
	int totalfordreds = 0;
	int totalfordblues = 0;
	int totalfordgreens = 0;
	
	int totalchevyreds = 0;
	int totalchevyblues = 0;
	int totalchevygreens = 0;

	//for loop to ask the user for the number of cars sold.
	for (int i = 0; i<CAR_TYPES; i++)
	{
		for (int j = 0; j<COLOR_TYPES; j++)
		{
			std::cout << "How many " << colors[j] << " " << carbrands[i] << "s were sold?" << endl;
			std::cin >> sales[j][i];
			
			if (sales[0][0] > 0) {
				totalfords = totalfords + sales[0][0];
				totalfordreds = totalfordreds + sales[0][0];
			}

			else if (sales[0][1] > 0) {
				totalfords = totalfords + sales[0][1];
				totalfordblues = totalfordblues + sales[0][1];
			}
			
			else if (sales[0][2] > 0) {
				totalfords = totalfords + sales[0][2];
				totalfordgreens = totalfordgreens + sales[0][2];
			}

			else if (sales[1][0] > 0) {
				totalchevy = totalchevy + sales[1][0];
				totalchevyreds = totalchevyreds + sales[1][0];
			}

			else if (sales[1][1] > 0) {
				totalchevy = totalchevy + sales[1][1];
				totalchevyblues = totalchevyblues + sales[1][1];
			}

			else if (sales[1][2] > 0) {
				totalchevy = totalchevy + sales[1][2];
				totalchevygreens = totalchevygreens + sales[1][2];
			}
		}
	}
	//print the total by car type
	std::cout << "Total Fords sold: " << totalfords << endl;
	std::cout << "Total Chevies sold: " << totalchevy << endl;

	//print the total by car color
	std::cout << "Total red cars sold: " << (totalfordreds + totalchevyreds) << endl;
	std::cout << "Total blue cars sold: " << (totalfordblues + totalchevyblues) << endl;
	std::cout << "Total green cars sold: " << (totalfordgreens + totalchevygreens) << endl;
	
	system("pause");
	return 0;
}
lines 6-12 replicate the sales array, which BTW, I don't see in your program.
You're doing the same thing twice.

Line 20: Take a look at your subscripting. You've changed this. It was sales[i][j], now it's sales[j][i]. i represents the car type, which is the first dimension of the array.

Lines 22-50: This works, but is rather brute force. It's also not easily extensible. If you were to add a car brand or a color, you have to recode your program.

I would suggest something like this:
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
#include <iostream>
using namespace std;

const int CAR_TYPES = 2;
const int COLOR_TYPES = 3;
const char * carbrands[] = 
    {   "Ford",
        "Chevy",
    };
const char * colors[] = 
    {   "Red",
        "Blue",
        "Green",
    };

int main()
{   int sales[CAR_TYPES][COLOR_TYPES];
    int total;
    int grand_total = 0;
    
	//for loop to ask the user for the number of cars sold.
	for (int i = 0; i<CAR_TYPES; i++)
	{   for (int j = 0; j<COLOR_TYPES; j++)
		{   cout << "How many " << colors[j] << " " << carbrands[i] << "s were sold? ";
		    cin >> sales[i][j];			
        }
    }
    
    //  Print the totals by car type    
    for (int i=0; i<CAR_TYPES; i++)
    {   total = 0;
        for (int j=0; j<COLOR_TYPES; j++)
            total += sales[i][j];
        cout << "Total " << carbrands[i] << "s sold = " << total << endl;
    }
    //  Print the totals by color
    for (int j=0; j<COLOR_TYPES; j++)
    {   total = 0;
        for (int i=0; i<CAR_TYPES; i++)
            total += sales[i][j];
        cout << "Total " << colors[j] << " cars sold = " << total << endl;
        grand_total += total;
    }        
	cout << "Grand total of " << grand_total << " cards sold" << endl;
	system("pause");
	return 0;
}


This is much more easily extensible. If you want to add more car brands or and/or colors, all you have to do is add an entry to the appropriate table and change the CAR_TYPE or COLOR_TYPE constants accordingly.
Last edited on
Thanks a bunch! For the record, I switched the "i" and "j" in sales because it produced results that were closer to the right answer. Oh well.
Topic archived. No new replies allowed.