-Write a C++ program to get the input of number of tickets for an amusement park. Each ticket costs $ 5.50. Every fifth ticket is free. If any person is less than 5 or greater than 65 (by age), then they get a discount of 1.08. Calculate the total price for group of visitors.
Specification: You can either use do-while / while / if-else if / switch
Example:
Enter number of visitors: 12
Your program should ask the age of every visitor.
Enter the age of 1st visitor – 68
You get a discount of 1.08
Enter the age of 2nd visitor – 50
Enter the age of 3rd visitor - 60
Enter the age of 4th visitor – 14
Your 5th ticket is free
Enter the age of 6th visitor – 10
Enter the age of 7th visitor – 8
Enter the age of 8th visitor – 4
You get a discount of 1.08
Enter the age of 9th visitor – 45
Your 10th ticket is free
Enter the age of 11th visitor – 68
You get a discount of 1.08
Enter the age of 12th visitor – 21
****************************************************
Your total price is : 51.76
****************************************************
Could someone please explain step by step how to carry out this small program, it would be much appreciated. :)
This is what I'd do:
Since you know that one in every five tickets is free, to find out if you're computing a fifth ticket in a row, you can divide the ticket number into 5. In this example, if you divide 12/5 and take just the integer part, you'll see that 2 of the 10 tickets are free.
So the first thing is to read the amount of tickets the user is charging for. Let's store that in a variable named "amount".
Now declare a variable for your total price to charge (let's call it "total") and then start looping to ask for the ages. Since you know how many tickets you're going to sell, iterate with a "for" loop, let's say for (i=1, i<=amount, i++)
For every iteration, you:
-Divide i%5 (modulus). if remainder is 0, then it's a 5th ticket in a row so you print it's a free ticket and go to the next iteration. If remainder is not 0, then you:
-Ask for the person's age.
-If the age is less than 5 or greater than 65, then you do 5.50-1.08 and add that to "total", and print the message saying there was a discount.
-If the age is between 5 and 65, you add 5.50 to "total" and print out the amount.
Once the loop ends, you print "total" as the total amount to charge.
Could you put an example code block of the iteration portion? I'm assuming that your useing cmath to do the division and the a cin to ask for the age followed by a math equation for the next two parts but am somewhat lost.
You actually don't need <cmath> for any of what you're trying to do. The language has already built in your standard math operators (aka addition + subtraction - multiplication * division / and modulo % )
Including <cmath> would be if you wanted to calculate the square root of a number, or maybe get a power, like pow(n,n). And there's other stuff but you don't really need to worry about those unless you're doing heavier computational stuff.