Functions without parameters

Summary: 6 companies have a product in 5 different warehouses. Each company is identified by a positive ID number and each warehouse is identified by a number (1 for the first, 2 for the second,…)
Object: the object of this assignment is to write a C++ program which asks the user to enter a company ID number, and the number of products in each of the warehouses. It then computes and prints the total number of products for that company
If the total number of product is less than 100, it prints the message “place a new order”
Input: for each company, its ID, and the number of products in each warehouse with appropriate prompt messages.
Example: Enter company ID number: 101
Enter number of products in warehouse #1: 30
Enter number of products in warehouse #2: 60
Enter number of products in warehouse #3: 0
Enter number of products in warehouse #4: 5
Enter number of products in warehouse #5: 27
The total for company 101 is: 122
Output: for each company, the total number of product, and the message “place a new order” if the total number of product is less than 100.

Method:
1. Define global variable, int total_prod to hold the total number of products for a company
2. define the function void compute_total() that uses a loop to read the number of the products in all warehouses for one company, computer the total number of products and store it into the global variable total_prod.
3. Define the function void new_order() that determines if a new order need to be placed as follows: if the total number of products (in the global variable total_prod) is less than 100, it prints the message “place a new order”
4. Your function main does the following in a loop:
- read a company ID number
- call function compute_total() to read the number of the product in all warehouses for that company, and to compute their sum
- print the total number of the product for that company with an appropriate message
- call the function new_order() to determine if a new order need to be placed.
Show us what you have. We don't do homework for others, but we will help you when you're stuck.
sorry was about to add that
#include <iostream>
using namespace std;

int total_prod = 0; // to hold the total number of products
void compute_total(int company[])
{
int check = 0;
while (check < 5)
{
total_prod += company[check];
check++;
}
}

void new_order()
{
if (total_prod < 100)
cout<<"Place a new order"<<endl;
}

int main()
{
int company1 [5];
int company2 [5];
int company3 [5];
int company4 [5];
int company5 [5];
int company6 [5];
int id1;
int id2;
int id3;
int id4;
int id5;
int id6;
int selectedID;

while (company < 106);
{
cout<<"Create the ID# for the first company: ";
cin>>id1;
cout<<"Enter the number of products in warehouse #1: ";
cin>>company1[0];
cout<<"Enter the number of products in warehouse #2: ";
cin>>company1[1];
cout<<"Enter the number of products in warehouse #3: ";
cin>>company1[2];
cout<<"Enter the number of products in warehouse #4: ";
cin>>company1[3];
cout<<"Enter the number of products in warehouse #5: ";
cin>>company1[4];
}


while (0==0)
{
cout<<"Select a company's ID#: ";
cin>>selectedID;

if (selectedID == id1)
{
compute_total(company1);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else if (selectedID == id2)
{
compute_total(company2);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else if (selectedID == id3)
{
compute_total(company3);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else if (selectedID == id4)
{
compute_total(company4);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else if (selectedID == id5)
{
compute_total(company5);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else if (selectedID == id6)
{
compute_total(company6);
cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
new_order();
}
else
{
cout<<"You did not select a valid ID#, try again"<<endl;
}

total_prod = 0;
}
}
i cant have the [] with numbers in them though so i am trying to find another way to do that
Please edit your post to include code tags. Highlight you code and then click the "<>" button on the Format menu. This will make it easier for us to read and comment on your code.

It will also provide line numbers, so you can post again an tell us which lines you are referring to in your previous post.

Maybe what you are struggling with is these lines:

1
2
3
4
5
6
int company1 [5];
int company2 [5];
int company3 [5];
int company4 [5];
int company5 [5];
int company6 [5];


These lines are each creating an array of 5 integers. So, this creates 30 integer variables named company1[0], company1[1], ... , company1[4], company2[0], ... , company6[4].

I doubt that's what you want. My guess is you really want to declare

int comapany[6] to create 106 variables. They you probably want to declare another array of id numbers for the companies int id[6].


Let my reconsider my answer.
Last edited on
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
using namespace std;



int total_prod = 0;

void compute_total(int company[])
{
	int check = 0;
	while (check < 5) 
	{
		total_prod = total_prod + company[check];
		check = check +1;
	}
}

void new_order()
{
	if (total_prod < 100) 
		cout<<"Place a new order"<<endl;
}

int main() 
{
	int company1 [5];
	int company2 [5];
	int company3 [5];
	int company4 [5];
	int company5 [5];
	int company6 [5];
	int id1;
	int id2;
	int id3;
	int id4;
	int id5;
	int id6;
	int selectedID;
	
	cout<<"Create the ID# for the first company: ";
	cin>>id1;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company1[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company1[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company1[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company1[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company1[4];
	
	cout<<"Create the ID# for the second company: ";
	cin>>id2;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company2[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company2[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company2[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company2[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company2[4];
	
	cout<<"Create the ID# for the third company: ";
	cin>>id3;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company3[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company3[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company3[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company3[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company3[4];
	
	cout<<"Create the ID# for the fourth company: ";
	cin>>id4;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company4[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company4[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company4[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company4[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company4[4];
	
	cout<<"Create the ID# for the fifth company: ";
	cin>>id5;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company5[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company5[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company5[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company5[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company5[4];
	
	cout<<"Create the ID# for the sixth company: ";
	cin>>id6;
	cout<<"Enter the number of products in warehouse #1: ";
	cin>>company6[0];
	cout<<"Enter the number of products in warehouse #2: ";
	cin>>company6[1];
	cout<<"Enter the number of products in warehouse #3: ";
	cin>>company6[2];
	cout<<"Enter the number of products in warehouse #4: ";
	cin>>company6[3];
	cout<<"Enter the number of products in warehouse #5: ";
	cin>>company6[4];
	
	while (0==0) 
	{
		cout<<"Select a company's ID#: ";
		cin>>selectedID;
		
		if (selectedID == id1) 
		{
			compute_total(company1);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else if (selectedID == id2) 
		{
			compute_total(company2);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else if (selectedID == id3) 
		{
			compute_total(company3);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else if (selectedID == id4) 
		{
			compute_total(company4);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else if (selectedID == id5) 
		{
			compute_total(company5);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else if (selectedID == id6) 
		{
			compute_total(company6);
			cout<<"The total for company #"<<selectedID<<" is: "<<total_prod<<endl;
			new_order();
		}
		else 
		{
			cout<<"You did not select a valid ID#, try again"<<endl;
		}
		
		total_prod = 0;
	}
}
having problems with: lines 26-31, 43, 45, 47, 49, 51
Thanks for reposting. I made some mistakes in my previous post. Please disregard it until I have a chance to review what you've got.
your welcome and okay thanks
the lines that you mentioned before are the ones that i am talking about i do not know how to do arrays and i need to find another way to write it so all the [] i need to change
Well, you really need to use arrays for this assignment. Check out the tutorial here: http://cplusplus.com/doc/tutorial/arrays/. That should get you enough to understand what you are doing.

However, instead of having 6 arrays in lines 26-31, you could have a 2-dimensional array. Something like this:

1
2
3
4
5
6
7
8
9
#define NUM_WAREHOUSES (5)
#define NUM_COMPANIES (6)

...


int product[NUM_COMPANIES][NUM_WAREHOUSES]
int companyId[NUM_COMPANIES];


Then you can loop through the companies and warehouses to populate the arrays.
i cant do anything else to replace or rewrite the problem? there isnt a second choice?
You would be able to replace lines 40 - 116 with something like:

1
2
3
4
5
6
7
8
9
10
for (int c = 0; c < NUM_COMPANIES; c++)
{
	cout<<"Create the ID# for company " << i << ": ";
	cin>>companyId[c];
        for (int w = 0; w < NUM_WAREHOUSES; w++)
        {
	        cout<<"Enter the number of products in warehouse #: " << w;
	        cin>>product[c][w];
        }
}

have not learned that, cant do that
Sorry, I didn't read the original problem that closely. I think you are doing too much work.

You don't need to read in all the data at the beginning (lines 40-116). I assumed you needed to do that from reading your code (my bad).

Let's look at the assignment again.

Method:
1. Define global variable, int total_prod to hold the total number of products for a company
2. define the function void compute_total() that uses a loop to read the number of the products in all warehouses for one company, computer the total number of products and store it into the global variable total_prod.
3. Define the function void new_order() that determines if a new order need to be placed as follows: if the total number of products (in the global variable total_prod) is less than 100, it prints the message “place a new order”
4. Your function main does the following in a loop:
- read a company ID number
- call function compute_total() to read the number of the product in all warehouses for that company, and to compute their sum
- print the total number of the product for that company with an appropriate message
- call the function new_order() to determine if a new order need to be placed.


Step 1 - define a global variable - check. I'm not thrilled with a global variable, but it is assigned.

Step 2 - Define compute_total - this function is supposed to "read the number of the products in all warehouses for one company", so it is this function where you would ask the user for each of the product counts. So, in our compute_total function you loop through the warehouses. And, there is no need to save the individual values for each warehouse, so all you need is 1 variable for reading the current count which is then added to the total.

Step 3 - Define new_order. check.

Step 4 - Define main. This step tells you exactly what main is supposed to do. You don't need to do anything more, unless you want to validate the company ID that the user inputs.

You've made main a lot more complicated than it needs to be.

Try stripping out what you don't need and post again.
thanks you i solved it :)
Topic archived. No new replies allowed.