It sounds like there are actually 3 functions. The two that are mentioned and the main() function.
The way I approach a problem like this is to read through the problem and start sketching out the bits that I know. I try to define the data and the functions (names, parameters, return values). Write the functionality as comments.
Let's start. The details of this problem start in the middle of the description:
The program must prompt the user for type of customer (‘p’ for pensioner, ‘s’ for student, ‘o’ for other) |
1 2 3 4 5
|
int main()
{
char customerType; // p, s, or o for pensioner, student or other
// Prompt for customer Type
|
as well as a variable indicating whether the person bought popcorn or not. |
Since it's an either/or choice, that sounds like a bool variable. Let's add it:
1 2 3 4 5 6
|
int main()
{
char customerType; // p, s, or o for pensioner, student or other
bool boughtPopcorn;
// Prompt for customer Type
|
It must then call the relevant function according to that entry. |
Hmm. Not sure what to make of this at first reading, but we'll have something like:
1 2 3 4 5
|
if (something) {
func1();
} else {
func2();
}
|
The first function must receive the customer type and indication if popcorn was bought and calculates discount for pensioners and students. |
Okay, let's translate that into a function prototype. Now various questions pop into my head at this point, so I add them to the comments:
1 2 3 4
|
// Given a customer type and whether they bought popcorn, calculate discount for pensioner and student
// Question: what exactly does this return? Percent discount? Discount amount? Discounted price?
// Question: Pensioner/student discount is the same, so why does this take cust type as a parameter?
double calcPSDiscount(char customerType, bool boughtPopcorn);
|
The second function receives an indication if popcorn was bought and calculates the discount for customers that are not pensioners or students. |
So this function and the one above should return the same sort of thing:
1 2
|
// Calcuate the discount for other customers:
double calcOtherDiscount(bool boughtPopcorn);
|
Putting this all together, I have:
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
|
// Given a customer type and whether they bought popcorn, calculate
// discount for pensioner and student
// Question: what exactly does this
// return? Percent discount? Discount amount? Discounted price?
// Question: Pensioner/student discount is the same, so why does this
// take cust type as a parameter?
double calcPSDiscount(char customerType, bool boughtPopcorn)
{
}
// Calcuate the discount for other customers:
double calcOtherDiscount(bool boughtPopcorn)
{
}
int main()
{
char customerType; // p, s, or o for pensioner, student or other
bool boughtPopcorn;
// Prompt for customer Type
if (something) {
func1();
}else {
func2();
}
}
|
Do you see how this starts to translate a problem statement into code? Now repeat the process. Re-read the problem statement, sentence by sentence and see what else you can fill in. On this second pass, you might add comments to calcPSDiscount() and calcOtherDiscount() to indicate how to compute the discounts. You'll notice that you don't have a comment for prompting for whether they bought popcorn. Finally, you'll probably refine the if statement:
1 2 3 4 5
|
if (customer type is pensioner or student) {
calcPSDiscount(customerType, boughtPopcorn);
} else {
calcOtherDiscount(boughtPopcorn);
}
|
What else is missing?
Keep repeating the process until you're pretty sure that you have everything covered. Then fill in code where you need it and start compiling.