NOT asking for whole question to be done just asking for the start off of it

Hi

So my main problem is starting a code off. I have the following question to write code for. If someone could please juts give me the starting lines of code i will be very grateful.
*****************************************************************************

Question

NuMetro has a special on movies for all members of the public but special discounts for students and pensioners. If pensioners or students buy a movie ticket they receive 10%. If they buy a movie and popcorns, they receive 20% discount. Other customers only receive a discount when they buy a movie ticket and popcorn - which is 5%; there is no discount for just a movie ticket alone. Write a program named question6b.cpp that will consist of two functions. The program must prompt the user for type of customer (‘p’ for pensioner, ‘s’ for student, ‘o’ for other) as well as a variable indicating whether the person bought popcorn or not. It must then call the relevant function according to that entry. The first function must receive the customer type and indication if popcorn was bought and calculates discount for pensioners and students. The second function receives an indication if popcorn was bought and calculates the discount for customers that are not pensioners or students.



A few notes to clarify question 6 of assignment 1.

You are requested to write a program that calculates and displays the final amount that is due for a movie ticket.
You can assume that the regular price for a movie ticket is R80-00. You must use this amount to calculate the discount and new ticket price.
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.
great thank you. I will work with that and see how far i can get
I definitely messed up. Looks like a dog had breakfast and then a party in it.


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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main()
{
int pensioner ('p');
int student ('s');
int other ('o');
int noDiscount;
int customerType;
float PSDiscount;

cout << "Enter the letter: " << endl;
cin >> customerType;

if ((customerType = ("s"))|| (customerType = ("p")))
{
    float calculatePSDiscount (float PSDiscount);
    PSDiscount = 80.00 - ( 80.00 * 0.10 );
}
else

if (customerType = 'o')
{
  return noDiscount; // do calculations for regular customers
}


{
  if (customerType = ("s") || customerType = ("p"))
  {
      float calculateDiscount (customerType; bool didBuyPopcorn)
    calculatePSDiscountwithPopcorn = ( 80.00 - ( 80.00 * 0.20))
  }
  else
  {
     if (customerType = 'o', bool didBuyPopcorn)
       ( 80.00 - ( 80.00 * 0.05));
  }

  float calculateOtherDiscountwithPopcorn (string customerType, bool didBuyPopcorn)

if (customerType = 'o', bool didBuyPopcorn)
{
     calculateOtherDiscountwithPopcorn ( 80.00 - ( 80.00 * 0.05) )
}
  else
{
    return noDiscount;
}
return 0;
}
}

Line 16: You've defined customerType to be an integer, so when you try to read it from cin, the program will reject any letter as a value. Keep in mind that in C++ programs, when you define a variable as being of a certain type, it can only contain values of that type. Int's can't contain strings. Strings can't contain numbers, etc. So I would change line 12 to char customerType;

Line 18. "s" is a string that contains one character. 's' is a single character. You need to compare customerType to the characters 's' and 'p', not the strings "s" and "p"

Line 20 declares a function called calculatePSDiscount(). To call that function, you should change this to calculatePSDiscount(). You'll want to pass some parameters to this right? And you need to do something with the return value, like store it in a variable.

Here's another trick that helps: try doing just one little piece at a time. For example, write a working program that just prompts for the customer type, whether they bought popcorn, and then prints out those two values (so you're sure you read them correctly). Get this program working. Then you can start adding the code that computes and displays the discounted price.
@halleyc

Remember, one '=' means assign, two '==', means compare. On lines 18, 25, 32, 39 and 45, you are wanting to compare, NOT assign.

Change lines 8 thru 12, from an int, to a char.
Still getting so many errors

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main()
{
int pensioner ('p');
int student ('s');
int other ('o');
int noDiscount;
char customerType;
float PSDiscount;

cout << "Enter the letter: " << endl;
cin >> customerType;

if ((customerType == 's')|| (customerType == 'p'))
{
    float calculatePSDiscount (float PSDiscount);
    PSDiscount = 80.00 - ( 80.00 * 0.10 );
}
else

if (customerType == 'o')
{
  return noDiscount; // do calculations for regular customers
}


  if (customerType == ('s') || customerType == 'p'))
  {
      float calculateDiscount (customerType; bool didBuyPopcorn)
    calculatePSDiscountwithPopcorn = ( 80.00 - ( 80.00 * 0.20))
  }
  else
  {
     if (customerType == 'o', bool didBuyPopcorn)
       ( 80.00 - ( 80.00 * 0.05));
  }

  float calculateOtherDiscountwithPopcorn (string customerType, bool didBuyPopcorn)

if (customerType == 'o', bool didBuyPopcorn)
{
     calculateOtherDiscountwithPopcorn ( 80.00 - ( 80.00 * 0.05) )
}
  else
{
    return noDiscount;
}
return 0;
}
}
There's a lot of code here that probably doesn't belong. I see four if statements. You only need one. You also have lots of variables that you don't use.

Here is your code with the junk removed. This almost compiles:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int
main()
{
    char customerType;

    cout << "Enter the letter: " << endl;
    cin >> customerType;

    if ((customerType == 's') || (customerType == 'p')) {
        calculatePSDiscount();
    } else {
        ;
    }

    return 0;
}

What should calculatePSDiscount() computer? The discount percentage? The discount in Rs? The discounted price? Pick one, document it in a command, and then write the code.

What code should be at line 17?

When all the computing is done, you still need to print out the answer.

Topic archived. No new replies allowed.