Adding Arrays to existing code

Hi all,

I hate to ask a homework related question but I am stumped and can't figure out how to add two arrays to my current project. My program already displays products and asks for quantities of each then returns a total, but this week I have to use arrays to do the same thing while keeping my current functions. Could anyone explain how to make it work, do I need to reference my arrays in my functions? I appreciate any help or nudge in the right direction.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #include <iostream>
#include <string>
using namespace std;

// Function Prototype
int getProduct(string);
int getQuantity(string);
int printSummary(int, int); //passes 2 variables to the function

// customer class definition
class Customer
{
	private:
		string customerName, customerAddress;
	
	public: //Public class functions
		void setName(string name) //Function to set cutomers name
		{
			customerName = name;	
		}
		string getName() //function to get customers name
		{
			return customerName;
		}
		void setAddress(string address) //Function to set customers address
		{
			customerAddress = address;
		}
		string getAddress() //Function to get customers address
		{
			return customerAddress;
		}
};

int main()
{
	//declare variables
	string name, address, anotherItem;
	int product, quantity;
	double totalValue = 0, itemValue = 0; 	
	
	Customer customer1; //creates a Customer object called customer1

	//Declare arrays product and quantity	
	string product[4];
	int quantity[4];
	
	cout << "Please enter your name: \n\n";
	
	getline(cin,name);
	customer1.setName(name); //Calls the setName function of the Customer Class
	
	cout << "Please enter your address: \n\n";
	
	getline(cin,address);
	customer1.setAddress(address); //Calls the setAddress function of the Customer Class
	
	do {
		cout << "Welcome " << customer1.getName() << "! to Lou's Cyber Store.'" << endl << endl;
		
		cout << "The address was entered as:  " << customer1.getAddress() << endl << endl;
		
		cout << "Please review items for purchase from the following menu:" << endl <<endl;
		
		cout << "(1) Bix 3 RC Airplane..............$225.00" << endl;
		cout << "(2) Delta Rocket RC Airplane.......$150.00" << endl;
		cout << "(3) Mini B-17 RC Airplane..........$275.00" << endl;
		cout << "(4) Cherokee Glider RC Airplane....$125.00" << endl << endl;
		
		
		
		//Function #1 Get the customers requested plane model
		product = getProduct ("Please enter the item number you would like to purchase: ");
		
		//Function #2 get customers required quantity
		quantity = getQuantity("Please enter the quantity you would like to purchase: ");
		
		// Function # 3 Display the customers order summary
		itemValue = printSummary(product, quantity);
		
		totalValue+=itemValue; //Running total
	
		cout << endl << "Your final cost of your entire order is: " << totalValue << endl;
		cout << endl << "Would you like to order another item? (Y/N)";
		cin >> anotherItem;
	
	} while ((anotherItem!= "n") && (anotherItem != "N"));
}   //Main program end

//Function section
//Function #1
int getProduct(string productPrompt)
{
	int productChoice;
	cout << productPrompt;
	cin >> productChoice;
	return productChoice;
}

//Function #2
int getQuantity (string quantityPrompt)
{
	int quantityChoice;
	cout << quantityPrompt;
	cin >> quantityChoice;
	return quantityChoice;
}

//Function #3
int printSummary (int product, int quantity)
{
	int itemValue;
	
	if (product==1)
	{
		cout << endl << "Product selection was: Bix 3  " << "Quantity was: " << quantity << endl
		<<"The total cost for this item is: " << quantity * 225 << endl;
		itemValue = quantity * 225;
		return itemValue;
	}
	else if (product==2)
	{
		cout << endl << "Product selection was: Delta  " << "Quantity was: " << quantity << endl
		<<"The total cost for this item is: " << quantity * 150 << endl;
		itemValue = quantity * 150;
		return itemValue;
	}
	else if (product==3)
	{
		cout << endl << "Product selection was: Mini B-17  " << "Quantity was: " << quantity << endl
		<<"The total cost for this item is: " << quantity * 275 << endl;
		itemValue = quantity * 255;
		return itemValue;
	}
	else if (product==4)
	{
		cout << endl << "Product selection was: Cherokee Glider  " << "Quantity was: " << quantity << endl
		<<"The total cost for this item is: " << quantity * 125 << endl;
		itemValue = quantity * 125;
		return itemValue;
	}
	else
	{
		return itemValue;
	}
}
I'm pretty sure you add:
int printSummary (int product[], int quantity[])
I forgot to add that I have to use loops to assign values to the arrays and loops to read and print values from the arrays, all while keeping my functions. If I do a loop to assign values won't it make my functions obsolete?
I'd start by looking at the areas where there is very repetitive code. These are candidates for the use of arrays.

Lines 116, 123, 130, 137 are almost identical. Also, values such as 225, 150, 125 appear on two lines each.

These are suggested things which could be stored in arrays:

Long description:
Bix 3 RC Airplane
Delta Rocket RC Airplane
Mini B-17 RC Airplane
Cherokee Glider RC Airplane

Short description:
Bix 3
Delta
Mini B-17
Cherokee Glider

Unit price:
225.00
150.00
275.00
125.00


You could create three separate arrays for these. Or define a product such as
1
2
3
4
5
struct Product {
    string Description;
    string shortDescr;
    double price;
}
and then use a single array of four Products. This way, you avoid having the price specified three times separately - with different values. Lines 67, 131 and 132 have 275.00, 275 and 255. Note the error which has crept in by having the values scattered throughout the code like this. Store the price just once in the array and these types of errors are eliminated.
If I do a loop to assign values won't it make my functions obsolete?

Your functions will certainly have to be re-worked. I suppose you could say that makes them obsolete. But you will still have functions, just changed ones.
@louser

I've compacted, and added to, your printSummary function. Also created two arrays.
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
116
117
118
119
120
121
122
123
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
// Declaring ONLY the parts of namespace we're actually using

// Function Prototype
int getProduct(string);
int getQuantity(string);
int printSummary(string products[4], int quantities[4], int product, int quantity); //passes 4 variables to the function

// customer class definition
class Customer
{
private:
	string customerName, customerAddress;

public: //Public class functions
	void setName(string name) //Function to set cutomers name
	{
		customerName = name;
	}
	string getName() //function to get customers name
	{
		return customerName;
	}
	void setAddress(string address) //Function to set customers address
	{
		customerAddress = address;
	}
	string getAddress() //Function to get customers address
	{
		return customerAddress;
	}
};

int main()
{
	//declare variables
	string name, address;
	char anotherItem; // Changed from string to char
	int product, quantity;
	double totalValue = 0, itemValue = 0;

	Customer customer1; //creates a Customer object called customer1

	//Declare arrays product and quantity	
	string products[4] = { "Bix 3", "Delta Rocket", "Mini B-17", "Cherokee Glider" };
	int quantities[4] = { 225, 150, 275, 125 };

	cout << "Please enter your name: \n\n";

	getline(cin, name);
	customer1.setName(name); //Calls the setName function of the Customer Class

	cout << "Please enter your address: \n\n";

	getline(cin, address);
	customer1.setAddress(address); //Calls the setAddress function of the Customer Class

	do {
		cout << "Welcome " << customer1.getName() << "! to Lou's Cyber Store.'" << endl << endl;

		cout << "The address was entered as:  " << customer1.getAddress() << endl << endl;

		cout << "Please review items for purchase from the following menu:" << endl << endl;

		for (int x = 0; x < 4; x++)
			cout << "(" << x + 1 << ") " << products[x] << " RC Airplane ......... $" << quantities[x] << endl;
		cout << endl;



		//Function #1 Get the customers requested plane model
		product = getProduct("Please enter the item number you would like to purchase: ");

		//Function #2 get customers required quantity
		quantity = getQuantity("Please enter the quantity you would like to purchase: ");

		// Function # 3 Display the customers order summary
		itemValue = printSummary(products, quantities, product, quantity);

		totalValue += itemValue; //Running total

		cout << endl << "Your final cost of your entire order is : $" << totalValue << endl;
		cout << endl << "Would you like to order another item? (Y/N)";
		cin >> anotherItem;

	} while (toupper(anotherItem) != 'N'); // Check just one input
}   //Main program end

//Function section
//Function #1
int getProduct(string productPrompt)
{
	int productChoice;
	cout << productPrompt;
	cin >> productChoice;
	return productChoice-1; // Reduce by 1, since arrays are zero based
}

//Function #2
int getQuantity(string quantityPrompt)
{
	int quantityChoice;
	cout << quantityPrompt;
	cin >> quantityChoice;
	return quantityChoice;
}

//Function #3
int printSummary(string products[4], int quantities[4], int product, int quantity)
{
	int itemValue;
	itemValue = quantity * quantities[product];

	cout << endl << "Product selection was : " << products[product] << " RC Airplane." << endl << "Quantity was: " << quantity << endl;
	cout << "The total cost for this item is : $" << itemValue << endl;
	return itemValue;
}



Whitenite1,

I see how you incorporated the arrays in my functions, that was were I was having trouble making the connection. Stupid question though, wouldn't quantity array be initialized with zeros then quantities of each item stored in the array? I see you used the price of each item in the array. I really appreciate everyone help!

V/r,

Lou
@louser

Actually, int quantities[4] = { 225, 150, 275, 125 };
should be renamed to something more meaningful, like int product_cost[4] = { 225, 150, 275, 125 }; You're using the variable, quantity, to just check how many are being purchased.

The way your program is right now, there's no reason to keep track of how many of each item is being purchased. If later, you decide to, you could add an array into your customer class, and fill it with your selections while the programs running.
One small comment, the original code had unit prices such as $225.00.
Note that although the number of cents happens to be zero, this is a type double, not int, which is reflected in the choice of double totalValue = 0, itemValue = 0;.

But the function printSummary()returns a result of type int, and uses the variable int itemValue;. The code should be consistent and use double throughout wherever dealing with dollar amounts.
@Whitenite1

I agree no need to use a quantities array but its a requirement for my class. That's where I am having the main issue to store quantities from a user and print the value with totals.

@Chevril,

That is a good catch, I completely missed it. I will change them all to double, makes sense especially when I add tax to the program.

Thanks for all your help guys!

@louser

Maybe this will help. I added in arrays for your customer, for costs and items.

You'll still have to change things to use doubles as Chevril mentioned.
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
// Declaring ONLY the parts of namespace we're actually using

// Function Prototype
int getProduct(string);
int getQuantity(string);
int printSummary(string products[4], int quantities[4], int product, int quantity); //passes 4 variables to the function

// customer class definition
class Customer
{
private:
	string customerName, customerAddress;

public: //Public class functions
	int Purchases[4];
	double Costs[4];
	void setName(string name) //Function to set cutomers name
	{
		customerName = name;
	}
	string getName() //function to get customers name
	{
		return customerName;
	}
	void setAddress(string address) //Function to set customers address
	{
		customerAddress = address;
	}
	string getAddress() //Function to get customers address
	{
		return customerAddress;
	}
};

int main()
{
	//declare variables
	string name, address;
	char anotherItem; // Changed from string to char
	int product, quantity, x;
	double totalValue = 0, itemValue = 0;

	Customer customer1; //creates a Customer object called customer1
	for (x = 0; x < 4; x++)// Zero out the arrays
	{
		customer1.Purchases[x] = 0;
		customer1.Costs[x]=0.0;
	}
	//Declare arrays product and quantity	
	string products[4] = { "Bix 3", "Delta Rocket", "Mini B-17", "Cherokee Glider" };
	int quantities[4] = { 225, 150, 275, 125 };

	cout << "Please enter your name: \n\n";

	getline(cin, name);
	customer1.setName(name); //Calls the setName function of the Customer Class

	cout << "Please enter your address: \n\n";

	getline(cin, address);
	customer1.setAddress(address); //Calls the setAddress function of the Customer Class

	do {
		cout << "Welcome " << customer1.getName() << "! to Lou's Cyber Store.'" << endl << endl;

		cout << "The address was entered as:  " << customer1.getAddress() << endl << endl;

		cout << "Please review items for purchase from the following menu:" << endl << endl;

		for (x = 0; x < 4; x++)
			cout << "(" << x + 1 << ") " << products[x] << " RC Airplane ......... $" << quantities[x] << endl;
		cout << endl;



		//Function #1 Get the customers requested plane model
		product = getProduct("Please enter the item number you would like to purchase: ");

		//Function #2 get customers required quantity
		quantity = getQuantity("Please enter the quantity you would like to purchase: ");

		// Function # 3 Display the customers order summary
		itemValue = printSummary(products, quantities, product, quantity);

		totalValue += itemValue; //Running total

		customer1.Purchases[product]+= quantity;

		customer1.Costs[product] += itemValue;
		//cout << endl << "Your final cost of your entire order is : $" << totalValue << endl;
                  // Not needed yet, as you'll show FINAL totalValue at end of program
		cout << endl << "Would you like to order another item? (Y/N)";
		cin >> anotherItem;

	} while (toupper(anotherItem) != 'N'); // Check just one input

	cout << customer1.getName() << ", you have finished shopping. Here are your totals" << endl;
	for (x = 0; x < 4; x++)
	{
		if (customer1.Purchases[x] > 0)
                {
			cout << "You puchased " << customer1.Purchases[x] << " " << products[x] << " RC Airplanes";
                  cout << " at a cost of $" << customer1.Costs[x] << ".00" << endl;
                 }
	}
	cout << "Total cost for your purchases was $" << totalValue << ".00" << endl << endl;

}   //Main program end

//Function section
//Function #1
int getProduct(string productPrompt)
{
	int productChoice;
	cout << productPrompt;
	cin >> productChoice;
	return productChoice-1;
}

//Function #2
int getQuantity(string quantityPrompt)
{
	int quantityChoice;
	cout << quantityPrompt;
	cin >> quantityChoice;
	return quantityChoice;
}

//Function #3
int printSummary(string products[4], int quantities[4], int product, int quantity)
{
	int itemValue;
	itemValue = quantity * quantities[product];

	cout << endl << "Product selection was : " << products[product]<< " RC Airplane.";
        cout << endl << "Quantity was: " << quantity << endl;
	cout << "The total cost for this item is : $" << itemValue << endl;
	return itemValue;
}
Last edited on
Topic archived. No new replies allowed.