My Program Got Problem In Calculating (1)

My program got problem while calculating,
it is about ordering drink,
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 <iostream>
#include <cstdlib>

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:"<<endl;
    cin>>name;
    cout<<"Hello, "<<name<<", "<<endl;
    cout<<"Welcome to drink order program"<<endl;
    cout<<"Please select a drink (enter number):"<<endl;
    cout<<"1. expresso"<<endl;
    cout<<"2. cappucino"<<endl;
    cout<<"3. latte"<<endl;
    cout<<"4. Hot chocolate"<<endl;
    cout<<"5. Plain Tea"<<endl;
    cin>>order;
    cout<<"Please enter amount:"<<endl;
    cin>>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"<<price<<endl;
    cout<<"Thank you for ordering!"<<endl;

    if (order == 1)
    {
        while (counter <= amount)
        {
            int counter = 1;
            if (sugar == 'y')
            {
                if (milk == 'y')
                {
                    float price = 2.50;
                    price = price + 1.00;
                }
                else if (milk == 'n')
                {
                    float price = 2.50;
                    price = price + 0.50;
                }
            }
            else if (sugar == 'n')
            {
                if (milk == 'y')
                {
                    float price = 2.50;
                    price = price + 0.50;
                }
                else if (milk == 'n')
                {
                    float price = 2.50;
                    price = price + 0;
                }
            }
            counter = counter + 1;
        }
        return price;
    }

    if (order == 2)
    {
        while (counter <= amount)
        {
            int counter = 1;
            float price = 2.50;
            if (sugar == 'y')
            {
                if (milk == 'y')
                {
                    price = price + 1.00;
                }
                else if (milk == 'n')
                {
                    price = price + 0.50;
                }
            }
            else if (sugar == 'n')
            {
                if (milk = 'y')
                {
                    price = price + 0.50;
                }
                else if (milk == 'n')
                {
                    price = price + 0;
                }
            }
            counter = counter + 1;
        }
        return price;
    }
    if (order == 3)
    {
        while (counter <= amount)
        {
            int counter = 1;
            float price = 2.50;
            if (sugar == 'y')
            {
                if (milk == 'y')
                {
                    price = price + 1.00;
                }
                else if (milk == 'n')
                {
                    price = price + 0.50;
                }
            }
            else if (sugar == 'n')
            {
                if (milk == 'y')
                {
                    price = price + 0.50;
                }
                else if (milk == 'n')
                {
                    price = price + 0;
                }
            }
            counter = counter + 1;
        }
        return price;
    }


    if (order == 4)
    {
        while (counter <= amount)
        {
            int counter = 1;
            float price = 2.00;
            if (sugar == 'y')
            {
                if (milk == 'y')
                {
                    price = price + 1.00;
                }
                else if (milk == 'n')
                {
                    price = price + 0.50;
                }
            }
            else if (sugar == 'n')
            {
                if (milk == 'y')
                {
                    price = price + 0.50;
                }
                else if (milk == 'n')
                {
                    price = price + 0;
                }
            }
            counter = counter + 1;
        }
        return price;
    }

    if (order == 5)
    {
        while (counter <= amount)
        {
            int counter = 1;
            float price = 1.50;
            if (sugar == 'y')
            {
                if (milk == 'y')
                {
                    price = price + 1.00;
                }
                else if (milk == 'n')
                {
                    price = price + 0.50;
                }
            }
            else if (sugar == 'n')
            {
                if (milk == 'y')
                {
                    price = price + 0.50;
                }
                else if (milk == 'n')
                {
                    price = price + 0;
                }
            }
            counter = counter + 1;
        }
        return price;
    }
}

Last edited on
order and amount should be int not long double price should be a double not long double.
1
2
    cout<<"The total price for your order: RM"<<price<<endl;
    cout<<"Thank you for ordering!"<<endl;
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:
1
2
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 <iostream>
#include <cstdlib>

using namespace std;
char name [20];
int order;
int amount;
double costorder;
int counter;
char sugar;
char milk;

int main()
{
    cout<<"Enter your name:"<<endl;
    cin>>name;
    cout<<"Hello, "<<name<<", "<<endl;
    cout<<"Welcome to drink order program"<<endl;
    cout<<"Please select a drink (enter number):"<<endl;
    cout<<"1. expresso"<<endl;
    cout<<"2. cappucino"<<endl;
    cout<<"3. latte"<<endl;
    cout<<"4. Hot chocolate"<<endl;
    cout<<"5. Plain Tea"<<endl;
    cin>>order;
    cout<<"Please enter amount:"<<endl;
    cin>>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"<<costorder<<endl;
    cout<<"Thank you for ordering!"<<endl;
}
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.
Thank you for answering again!
Last edited on
Topic archived. No new replies allowed.