Need some advice about an array.

I need some advice with implementing a second array in my program. What I have been asked to do is: Ask the customer to select products and quantities and save the provided details in arrays then Read from the arrays to print the order summary; that is, the products, quantities, and the total price for each product Calculate and print the total price for the order.I think I have implemented an array correctly for the quantities, but I can't seem to figure out a way to implement one for the products. I have tried changing my switch case statement around to implement an array for the products with no luck, so i reverted back to my last working program to start over. Not sure where to go from here. Any advice as to how I can go about doing this and or suggestions where I should start would be greatly appreciated. Thanks for any help in advance. My code below is my last working program I reverted back to.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 #include <iostream>
#include <string>
using namespace std;
//menu
void displayProducts()
{
	int total;
	int a[3];
	int i;
	for (i = 0;i < 3; i++)
		a[i] = 0;
	char selection = 'a';
	do
	{

		cout << "Please select a product from the list below. Each item cost $1" << endl;
		cout << "Menu";
		cout << "======" << endl;
		cout << "1 - Apples" << endl;
		cout << "2 - Bananas" << endl;
		cout << "3 - Oranges" << endl;
		cout << "E - Press E to proceed to order summary." << endl << endl;
		cout << "Enter selection: ";
		// read user selection
		cin >> selection;


		switch (selection)
		{
		case '1':
			cout << "You selected - Apples" << endl;
			cout << "Please enter quantity. ";
			cin >> a[0];
			break;

		case '2':
			cout << "You selected - Bananas" << endl;
			cout << "Please enter quantity. ";
			cin >> a[1];
			break;

		case '3':
			cout << "You selected - Oranges" << endl;
			cout << "Please enter quantity. ";
			cin >> a[2];
			break;

		case 'E':
		case 'e':
			cout << "Thank you." << endl;
			break;

			//if  1, 2, 3 or E is not selected then go to the default case
		default: cout << "Invalid selection. Please try again";
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'E' && selection != 'e');

	cout << "\nYou ordered " << a[0] << " Apples" << " for a cost of $"<< a[0] << endl;
	cout << "You ordered " << a[1] << " Bananas" << " for a cost of $" << a[1] << endl;
	cout << "You ordered " << a[2] << " Oranges" << " for a cost of $" << a[2] << endl;

	total = (a[0] + a[1] + a[2]);
	cout << "Your grand total is $" << total << endl;
	cin.get();
}
// Customer class
class Customer
{
	string name;
	string address;
public:
	void setName(string);
	string getName();
	void setAddress(string);
	string getAddress();
};
// Function definitions of customer class
void Customer::setName(string name1)
{
	name = name1;
}
void Customer::setAddress(string address1)
{
	address = address1;
}
string Customer::getName()
{
	return name;
}
string Customer::getAddress()
{
	return address;
}

int main()
{
	Customer a;
	string name;
	string address;
	cout << "Please enter your name." << endl;
	getline(cin, name);
	cout << "Please enter your address." << endl;
	getline(cin, name);
	cout << "hello " << name << endl;
	char selection = 'a';
	displayProducts();

	cin.get();
	return 0;
}


Line 68-95: What is the point of your Customer class? You don't use it.

Line 8: a[3] is a poor name for an array that contains quantities. qty[3] would be a better choice.

Not clear from what you posted, what the prices of your various fruits are. I would suggest you create an array for prices.
 
  const double prices[3] = { 0.75, 0.50, 1.0 };


Lines 13-56: What happens if the user selects the same choice on a subsequent pass through the loop. You reset the quantity. Wouldn't it be better to add to the existing quantity?

Lines 60-62: You're assuming the price is the same as the quantity. Unlikely. You can calculate costs inline, or you can create an array for costs.
1
2
3
  double costs[3];
  for (int i=0; i<3; i++)
    costs[i] = qty[i] * price[i];


Lines 60-62 become:
1
2
3
  cout << "You ordered " << qty[0] << " Apples for a cost of $"<< cost[0] << endl;
  cout << "You ordered " << qty[1] << " Bananas for a cost of $" << cost[1] << endl;
  cout << "You ordered " << qty[2] << " Oranges for a cost of $" << cost[2] << endl;










Topic archived. No new replies allowed.