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
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
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;
}
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.