Function problem SOS?

ok
i am new at function plz help me
i wrote this program for a shoe distribution company ( classroom problem) where i have 5 salespersons. each salesperson got an ID (1-5)
i sum all sales from each salesmen sales

plz help me out to get it into a void function or functions
i think i need 5 but i dont know how

here is the code

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

#include <iostream>
#include <iomanip>

using namespace std;



int main()
{
	int id=0;
	double sale=0;


	double sum1=0.0;
	double sum2=0.0;
	double sum3=0.0;
	double sum4=0.0;
	double sum5=0.0;
	

	while (id != -1)
	{
		
		cout<<"Enter sales ID number: ";
		cin>>id;


		if(id>=1 && id <=5)
		
		switch (id){

		case 1:
			
		cout<<"Enter sales amount: ";
		cin>>sale;
		if(sale>=0)
		sum1 = sum1 + sale;
		else
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter sales amount: ";
			cin>>sale;
			sum1 = sum1 + sale;
		
		}
			break;

		case 2:
		
			cout<<"Enter sales amount:";
			cin>>sale;
			if(sale>=0)
			sum2 = sum2 + sale;
					
			else
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter sales amount: ";
			cin>>sale;
			sum2 = sum2 + sale;
		
		}
		
		break;

		case 3:
		
		cout<<"Enter sales amount:";
		cin>>sale;
		if(sale>=0)
		sum3 = sum3 + sale;
				
		else
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter sales amount: ";
			cin>>sale;
			sum3 = sum3 + sale;
		
		}
	
		break;

		case 4:
		
		cout<<"Enter sales amount:";
		cin>>sale;
		if(sale>=0)
		sum4 = sum4 + sale;
		
		else
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter sales amount: ";
			cin>>sale;
			sum4 = sum4 + sale;
		
		}
		
		break;

		case 5:
		
		cout<<"Enter sales amount:";
		cin>>sale;
		if(sale>=0)
		sum5 = sum5 + sale;
				
		else
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter sales amount: ";
			cin>>sale;
			sum5 = sum5 + sale;
		
		}

			


		}//end of swtich
					else
			cout<<"InvaliD ID Entered\n"<<endl;


	}//end while loop

	cout<<"The sales total for each of the salespersons is: \n";
	cout<<right<<setw(7)<<"1"<<"       2"<<"        3"<<"      4"<<"       5"<<endl;
	cout<< fixed <<showpoint<<setprecision(2)<<sum1<<" "<<sum2<<" "<<" "<<sum3<<" "<<sum4<<"  "<<sum5<<endl;


    return 0;
}//end main function

1
2
3
4
5
6
7
8
void someFunction() {
// Do something
}

int main() {
 // Call the function
 someFunction();
}
Last edited on
OK, one of the basic uses of functions is to replace repeated code.
In your example you have a switch statement with almost the same code for each case - so with a bit of thought you can replace this with a function call which does the work of the repeated code.
See http://www.cplusplus.com/doc/tutorial/functions.html for info on functions.
I would replace the entry of sales amount by a function as a first step.
EG
1
2
3
4
5
6
7
8
9
10
11
12
double entersales()
{
    double sales;
    count << "Enter Sales" ;
    cin >> sales;
    if (sales < 0)
    {
         cout << "Invalid, please enter positive amount";
         cin >> sales;
    }
    return sales;
}

which you would call with code such as
1
2
3
4
switch (id)
case 1: sum1 += entersales();
case 2: sum2 += entersales();
...



Well, you could simplify everything by using Arrays.

Then the thing should look somehow like this.

regards

int main()

last edit: I had forgotten the code-tags
______________
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
int Enter_ID();

double Summation(int ID);

int main()
{
int ID;
double Sum[5] = {0,0,0,0,0,0};
char ch='y';

while {ch!='n')
   { 
   ID = Enter_ID();
   Sum[ID] += Summation();
   cout<<"Another Salesguy? (y/n)";
   cin.get(ch);
   }
// code or the output
cin.get();
}
int Enter_ID();
{
int ID
//Code to enter the ID

return ID;
}

double Summation();
{
double WhatToAdd;
// Code to enter the Amount to be added
return WhatToAdd;
}
Last edited on
Thanks
i havent got to arrays yet
but thanks
thanks alot
this is so much easier
it looks less complicated
it would be cool when i get into arrays
i have no idea how to use it LOL
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

#include <iostream>
#include <iomanip>

using namespace std;

 double entersales();

double sale=0;

int main()
{
	int id=0;
	double sum1=0.0;
	double sum2=0.0;
	double sum3=0.0;
	double sum4=0.0;
	double sum5=0.0;
	

	while (id != -1)
	{
		
		cout<<"Enter sales ID number: ";
		cin>>id;

		if(id>=1 && id <=5)
		
		switch (id){

		case 1:
			entersales();
			sum1 = sum1 + sale;

			break;

		case 2:
			entersales();
			sum2 = sum2 + sale;
		
			break;

		case 3:
			entersales();
			sum3 = sum3 + sale;		
	
		break;

		case 4:
			entersales();
			sum4 = sum4 + sale;
			
		
		break;

		case 5:		
			entersales();
			sum5 = sum5 + sale;		

		}//end of swtich					
		else

			cout<<"InvaliD ID Entered\n"<<endl;

	}//end while loop

	cout<<"The sales total for each of the salespersons is: \n";
	cout<<right<<setw(7)<<"1"<<"       2"<<"        3"<<"      4"<<"       5"<<endl;
	cout<< fixed <<showpoint<<setprecision(2)<<sum1<<" "<<sum2<<" "<<" "<<sum3<<" "<<sum4<<"  "<<sum5<<endl;

    return 0;
}//end main function

double entersales()
{		
		cout<<"Enter sales amount: ";
		cin>>sale;
		while ( sale < 0)
		{
			cout<<"Invalid amount enter."<<endl;
			cout<<"Enter positive sales amount: ";
			cin>>sale;
		
		
		}
		return sale;
}//end function



Last edited on
Looks good so far. One thing you can change is.

1
2
if(id>=1 && id <=5)
switch (id){


You don't actually need that if-else Statement in there. A Switch statement has the default: option that will cover all.

e.g.
1
2
3
4
5
6
7
8
switch(id) {
 case 1:
  // do something
  break;
 default:
  cout << "invalid id" << endl;
  break;
}

Last edited on
thanks alot
it is better LOL
Last edited on
Apart from that it looks pretty good. Next step would be to turn it into an array based application. That'd reduce your code by maybe half. :)
help me?
lol
i am reading arrays right now
See http://www.cplusplus.com/doc/tutorial/arrays.html for the docs on this site on arrays.
I would replace
1
2
3
4
5
   double sum1=0.0;
   double sum2=0.0;
   double sum3=0.0;
   double sum4=0.0;
   double sum5=0.0;

with
 
   double sum[5] = {0.0,0.0,0.0,0.0,0.0};

You can then remove the whole switch statement and just have
 
  sum[id-1] = sum[id-1] + entersales();   //Array is from 0 to 4 so use (id-1) 

BUT you DO need to keep the if..else if you make this change, so as to ensure you always index the array properly
You need to modify the final output to use the array as well.
Topic archived. No new replies allowed.