### My Program Got Problem In Calculating (1)

My program got problem while calculating,
but each time when I want to calculate,
it will show 0 in the result,
can anyone helo me to solve the problem?

My code is as follows:
 ``` #include #include using namespace std; char name [20]; long double order; long double amount; int counter; char sugar; char milk; long double price; int main() { cout<<"Enter your name:"<>name; cout<<"Hello, "<>order; cout<<"Please enter amount:"<>amount; cout<<"Do you wish to add sugar? (y/n)"; cin>>sugar; cout<<"Do you wish to add milk? (y/n)"; cin>>milk; cout<<"The total price for your order: RM"<
Last edited on
order and amount should be `int` not `long double` price should be a `double` not `long double`.
 ``12`` `````` cout<<"The total price for your order: RM"<
should be after you calculate the price not before.

You don't need those arbitrary return statements.
You should use basic math instead of while loops.

Your logic is also very confusing. I would do this:
1) create an array with the prices of each coffee example:
 ``12`` ``````std::size_t const numberOfCoffees = 5; double const prices[numberOfCoffees] = { 1.0, 2.0, 3.0, 4.0, 5.0 };``````
then you can simply get the cost of the order like: `double costOfOrder = amount * prices[order-1] + amount * costOfMilk * (milk == 'y') + amount * costOfSugar * (sugar == 'y');`
Last edited on
Thanks giblit!
Another error is
error: invalid types 'const float [5][double]' for array subscript|
You must use an integer for the subscript. If you think about it how would you have an offset of 2.5 there is only 2 or 3.
After I changed to int type, the error still occurs.
Here is the code:
 ```#include #include using namespace std; char name [20]; int order; int amount; double costorder; int counter; char sugar; char milk; int main() { cout<<"Enter your name:"<>name; cout<<"Hello, "<>order; cout<<"Please enter amount:"<>amount; cout<<"Do you wish to add sugar? (y/n)"; cin>>sugar; cout<<"Do you wish to add milk? (y/n)"; cin>>milk; std::size_t const numberOfCoffees = 5; double const prices[numberOfCoffees] = { 2.50, 2.50, 2.50, 2.00, 1.00 }; double costorder = amount * prices + amount * 0.50 * (milk == 'y') + amount * 0.50 * (sugar == 'y'); cout<<"The total price for your order: RM"<
Last edited on
And sorry for keep asking becoz im just a beginner.
Anyway, the question is as follows:
You are to create a program to manage an order. To place an order the users have to choose
among the following 5 items:
No Item RM
1 Expresso 2.50
2 Cappucino 2.50
3 Latte 2.50
4 Hot Chocolate 2.00
5 Plain Tea 1.00

The program will allow the user to enter a number between 1-5 and the quantity. The users
will also have a choice to add extra milk and extra sugar with an additional price of 50 cent
each.
Your program will output which beverage they have ordered, the quantity and the total
amount that they need to pay. Include error handling in your program should the choice
entered is outside of the range. Your program can handle any changes to be made to the order
should the users change their mind before making the payment.
Last edited on
please use code tags and not quote. either the <> button or put [ code] before and [/code] after (without the space). The problem is probably this line: `double costorder = amount * prices + amount * 0.50 * (milk == 'y') + amount * 0.50 * (sugar == 'y');` it should be `prices[order - 1]` You also are not following the assignment 100% It asks for you to
 Your program will output which beverage they have ordered, the quantity and the total amount that they need to pay. Include error handling in your program should the choice entered is outside of the range. Your program can handle any changes to be made to the order should the users change their mind before making the payment.