You are given a task to write a computer program for a Coffee Shop.  Following is the table containing the list of product that you sell.   Please be informed that all sales must include Sales Tax of 5%. For example if the customer orders one coffee with ice and creamer the total price is
Total Price = (1 * $1.50 + $0.30 + $0.50) * 1.05 = $2.42.
Your program must get the order from the customer and calculate the Total Price of his/her order. Customer can order multiple products at one time and your program must calculate the Total Price for all his/her order.
Product   Base Price   Add Ice  Add Creamer  Ice Blended(ice is   
                                                                        included)
coffee       $1.50        +$0.30    +$0.50           +$1.20
tea          $1.20        +$0.20     +$0.30           +$1.00 
For all sales, there is a sales tax of 5%
1. Draw Flowchart diagram
2. Write a console program using C++ programming language.

Well what do you need help with? Please post the code you attempted and highlight any sections and explain the problems.

I find it difficult to find a way in which way we will calculate the total cost, if a person can order more products at the same time,and the price of extras for tea and coffee is different. Currently I am in a vicious circle.

The hint here is that you need to draw a flow chart. Rather than thinking of a straight line, you will need to break off and create two parallel flows.

Imagine you're standing in line at the coffee shop, the first thing the cashier asks is "What would you like?" In this case you have two options, tea or coffee. So you choose Coffee and the cashier inputs that on their till. This has started the "Coffee" flow. Then they will ask, "Would you like any extras such as Ice or Cream?" if you say "Ice", they then might ask "Would you like it blended?" All this while they're inputting your answers into the till which is totting up the total. At the end they ask "Would you like anything else?" Which begins the process again.

You need to apply this logic to your application. If you're struggling with the concept in code, I guarantee you will find it much easier to write the flow down first and write the code based on the flow.

commented: Very good advice +15

Thank you, your advice helped me a lot, but I still have a problem with total_cost, how we can summ total_cost if the process is repeated several times.

this is what Ihave already done, but it is not finish tea

#include <iostream>
using namespace std;

int  main()
{ 
  float coffeePrice= 1.5, coffeeIce= 0.3, coffeeCreamer= 0.5, coffeeiceblanded= 1.2;

  float teaPrice= 1.2, teaIce= 0.2, teaCreamer= 0.3, teaIceblanded= 1.00;

  float total_cost= 0.0;

  char order, order1, wish, wish1;


 do 
  {
    cout<< "What would you like Coffee or Tea? Press C for Coffee or T for Tea.";

    cin>> order;
    total_cost= coffeePrice;

    if (order='C')

    { 
      cout<<"Would you like any additional, extraxs such as Ice or Creamer or both? Press I for ice or C for cremaer or B for both.";

      cin>> order1;

      if (order1='I')

      cout<<"Wolud you like ice blanded?(YorN)";
      cin>> wish;

      if( wish='N')
      total_cost= coffeePrice+coffeeIce;
        else
        total_cost= coffeePrice+ coffeeiceblanded;

        if (order1='C')
           total_cost= coffeePrice+coffeeCreamer;

         if (order1='B') 
             total_cost= coffeePrice+coffeeCreamer+coffeeIce; 



    }


    else 

    cout<<"Would you like any additonal for tea as"; 




    cout<< "Would you like anything else? (YorN):";
    cin>> wish1 ;

}

while (wish1='Y');

cout<< "Total cost of your order is"<<total_cost;
return 0;

} 

in line 20 change total_cost= coffeePrice; to total_cost += coffeePrice; This will keep adding additional items to total_cost until the program exits. If you plan on creating another order before the program exits just set total_cost to zero when you start the new order.

A couple of notes:

if (order='C') should probably read if (order =='C') a = is the assignment operator and a == is the equality operator. This applies to all your if and while statements.

You also might find it easier down the road to change your code to using a menu instead of a chain of inputs. This not only simplifies your code and makes it more modular, but, it also makes it easier to maintain it down the road. This is particularly useful if you're doing an assignment for a course. One thing that some instructors will do is have you change and refine an older assignment, which means you have to learn what it's doing all over again. Having your code in an organized modular format makes this process much easier.

I have written other code, but there is error, I can't see where is the problem. Could anybody tell me?

#include <iostream>
using namespace std;

int main ()


{


    int extrasC, extrasT;

    float pCoffee=1.50, pIceC=0.30, pCreamerC=0.50, pIceBC=1.20;

    float pTea=1.20, pIceT=0.20, pCreamerT=0.30, pIceBT=1.00;

    float cost=0.0, toPay=0.0;

    char choice;



    do

    {
        cout<< "Hello, please enter the lettet for your choice!" <<endl;

        cout<< "C Coffee"<<endl;

        cout<<"T Tea"<<endl;

        cout<<"D You are done with order"<<endl;

        cin>>choice;

        switch(choice)
        {

        //

        case 'T':
            cout<<"Please choose your extras with your Tea:"<<endl;
            cout<<"0 NO extras"<<endl;
            cout<<"1 Only ICE"<<endl;
            cout<<"2 Only CREAMER"<<endl;
            cout<<"3 ICE & CREAMER"<<endl;
            cout<<"4 ICE blended"<<endl;

            cin>>extrasT;

            switch(extrasT)
            {
                case 0:

                     cout<<"You don't want any extras\n"<<endl;

                     cost=pTea;

                break;

                case 1:

                      cout<<"We will add ice to your tea\n"<<endl;

                      cost=pTea+ pIceT;

                 break;

                 case 2:

                      cout<<"You added Creamer to your tea\n"<<endl;

                      cost=pTea+pCreamerT;

                  break;

                 case 3:

                      cout<<"You added Ice and Creamer to your tea\n"<<endl;

                      cost=pTea+ pIceT+ pCreamerT;

                  break;

                  case 4:

                       cout<<"You added Ice blended\n"<<endl;

                       cost= pCoffee + pIceBT;

                   break;

                   default:

                      cout<<"You made no choice\n"<<endl;
                  }

                //

                case ('C'):

                  cout<<"Please choose your extras with your Coffee:"<<endl;

                  cout<<"0 NO extras"<<endl;

                  cout<<"1 Only ICE"<<endl;

                  cout<<"2 Only CREAMER"<<endl;

                  cout<<"3 ICE & CREAMER"<<endl; 

                  cout<<"$ ICE blended"<<endl;

                  cin>> extrasC;

                  switch(extrasC)
                  {
                    case 0:

                        cout<<"You don't want any extras\n"<<endl;

                        cost= pCoffee;

                     break;

                      case 1:

                          cout<<"We will add Ice to your Coffee\n"<<endl;

                          cost= pCoffee + pIceC;

                       break;

                       case 2:

                          cout<<"You addeed Creamer to your Coffee\n"<<endl;

                          cost=pCoffee + pCreamerC;

                        break;

                        case 3:

                            cout<<"You added Ice and Creamer to your Coffee\n"<<endl;

                            cost= pCoffee + pIceC + pCreamerC;

                        break;

                        case 4:

                            cout<<"You added Ice blended\n"<<endl;

                            cost=pCoffee + pIceBC;

                        break;

                        default:

                            cout<<"You made no choice\n"<<endl;

                  }


                 //

                 toPay=toPay+cost;
                 cost=0.0;

                 //

                 case ('D'):

                    cout<<"You are done with order\n"<<endl;

                break;

                default:

                   cout<<"The letter you entered is not an option in the list!"<<endl;                                  

                    }

           cout<<"Would you like any other order\n"<<endl;

        }

         while(choice!='D');

         //

         cout<<"\n *****Thank you for visiting us *****"<<endl;

         cout<<"\nYour amount is:>>>"<<(toPay*0.05)<<"<<<"<<endl;

         cout<<"\n***********************************************"<<endl;

         return 0;





 }

Can you please specify what the error is?

Thanks a lot, I solved the error. But, This program still doesn't calculate my total cost. Why?

You're missing several break statements which cause the code to 'fall through' from one case to the next.

break essentially tells it to stop executing instructions in the current switch statement and continue execution at the next instruction after last brace } of the switch statement.

Example:

switch(myChoice)
{
    case 0:
        cout << "You picked 0" << endl;
        break;
    case 1:
        cout << "You picked 1" << endl;
    case 2:
        cout << "You picked 2" << endl;
}
cout << "Finished" << endl;

I have intentionally left the break statement out of case 1. So the output here is:

Input:
    myChoice = 0;
Output:
    You picked 0
    Finished

Input:
    myChoice = 1;
Output:
    You picked 1
    You picked 2
    Finished

Input:
    myChoice = 2;
Output:
    You picked 2
    Finished

Thank you, I fixed "break", but probably not very well, because if my first choice is T,program after T put me choices for C as well. Also this program doesn't calculate my toPay.

If you're still getting into the "C" options, it means you're missing a break after the "T" section.

To make life a bit easier, you can surround your case statements in brackets. Personally, I think a case statement should never be more than 3-5 lines long. If it is, call a method :)

If that's outside the remit of your project, fair enough, in this case use brackets to see when your code starts and ends.

Example:

switch(choice)
{
    case 0:
    {
        cout << "Choice 0" << end;
        total += 1;
        subTotal += 1;
        break;
    }
}

Just makes it easier to read and should help you find where your missing break statements are.

I fixed a definitly "break", and it is no longer a problem, the only problem now is that this program doesn't disply the sum of money person has to pay. Does anyone know why?

They way you calculate your tax is wrong. Multiplying by a number less than one is analagous with dividing. So multiplying by 0.05 will give you the same answer as dividing by 20.

You can simplyify decimal multiplcation by treating both sides of the decimal as different components of the equation and then adding them together.

So;

2 * 0.05 = 2 / 20 = 0.1
2 * 1.05 = (2 * 1) + (2 * 0.05) = 2.1

or even;

2 * 2.05 = (2 * 2) + (2 * 0.05) = 4.1

if you want to simplify the second part (for mental arithmatic), multiply the right side by 10^x till it becomes a whole number and divide that result by the same factor of ten;

2 * 6.05 = (2 * 6) + ( 2 * (0.05 * 100)) / 10 = 12 + ((2 * 5) / 100)
= 12 + (10 / 100) = 12 + 0.1 = 12.1

Can even do it with bigger numbers;

25 * 14.62 = (25 * 14) + ((25 * (0.62 * 100)) / 100)
           = (250 + 100) + ( (25 * 62) / 100)
           = 350 + (1550 / 100)
           = 350 + 15.5
           = 365.5

Also, while I'm at it, simplified way to do mental multiplication with large numbers.

From above we had 25 * 62. This will do...

First do the units of the multiplier;

25 * 62 = (25 * 60) + (25 * 2) = (25 * 60) + 50

Secondly, you can do the "tens" as two stages. First work it out using only the unit value, then multiply the answer by the units factor of ten;

(25 * 60) = (25 * 6) * 10 = ((20 * 6) + (5 * 6)) * 10
          = (120 + 30) * 10
          = 150 * 10
          = 1500

Add back the unit value we previously worked out and you have your result :)

1500 + 50 = 1550 = 25 * 62

OK, I know what it means when you multiply by one half. This was due to the speed. The problem is when I press D that means I finished oreder, program ends without writing any of this

 cout<<"\n *****Thank you for visiting us *****"<<endl;
         cout<<"\nYour amount is:>>>"<<(toPay*0.05)<<"<<<"<<endl;
         cout<<"\n***********************************************"<<endl;

If it's just that the console disappears before you see it, try adding _getch() just before you return from main. This will wait for you to press enter. You will need to include 'conio.h'

PS. I understand that _getch() used this way is pretty bad practice, however, I don't believe that for this assignment it's worth building in classes that allow the OP to flush the input stream. Just seems a bit overkill ;)

if I put toPay+=(toPay*o.o5), it will be Ok , won't i? But I still don't have any of those

 cout<<"\n *****Thank you for visiting us *****"<<endl;
         cout<<"\nYour amount is:>>>"<<(toPay+=(toPay*0.05))<<"<<<"<<endl;
         cout<<"\n***********************************************"<<endl;

Now I am confused, I don't know what I am doing wrong all the time.

Can you post your new code block please? I know it printed it out last time so I'm not sure what you're doing wrong this time.

This is new code with correct "break"

#include <iostream>
using namespace std;

int main ()
{


    int extrasC, extrasT;

    float pCoffee=1.50, pIceC=0.30, pCreamerC=0.50, pIceBC=1.20;

    float pTea=1.20, pIceT=0.20, pCreamerT=0.30, pIceBT=1.00;

    float cost=0.0, toPay=0.0;

    char choice;



    do

    {
        cout<< "Hello, please enter the lettet for your choice!" <<endl;

        cout<< "C Coffee"<<endl;

        cout<<"T Tea"<<endl;

        cout<<"D You are done with order"<<endl;

        cin>>choice;

        switch(choice)
        {

        //

        case ('T'):
            cout<<"Please choose your extras with your Tea:"<<endl;
            cout<<"0 NO extras"<<endl;
            cout<<"1 Only ICE"<<endl;
            cout<<"2 Only CREAMER"<<endl;
            cout<<"3 ICE & CREAMER"<<endl;
            cout<<"4 ICE blended"<<endl;

            cin>>extrasT;

            switch(extrasT)
            {
                case ('0'):

                     cout<<"You don't want any extras\n"<<endl;

                     cost=pTea;

                    break;

                case ('1'):

                      cout<<"We will add ice to your tea\n"<<endl;

                      cost=pTea+ pIceT;

                      break;

                 case ('2'):

                      cout<<"You added Creamer to your tea\n"<<endl;

                      cost=pTea+pCreamerT;

                      break;

                 case ('3'):

                      cout<<"You added Ice and Creamer to your tea\n"<<endl;

                      cost=pTea+ pIceT+ pCreamerT;

                      break;

                  case ('4'):

                       cout<<"You added Ice blended\n"<<endl;

                       cost= pCoffee + pIceBT;

                       break;

                   default:

                      cout<<"You made no choice\n"<<endl;

                  }

         break;

                //

                case ('C'):

                  cout<<"Please choose your extras with your Coffee:"<<endl;

                  cout<<"0 NO extras"<<endl;

                  cout<<"1 Only ICE"<<endl;

                  cout<<"2 Only CREAMER"<<endl;

                  cout<<"3 ICE & CREAMER"<<endl; 

                  cout<<"$ ICE blended"<<endl;

                  cin>> extrasC;

                  switch(extrasC)
                  {
                    case ('0'):

                        cout<<"You don't want any extras\n"<<endl;

                        cost= pCoffee;

                        break;

                      case ('1'):

                          cout<<"We will add Ice to your Coffee\n"<<endl;

                          cost= pCoffee + pIceC;

                          break;

                       case ('2'):

                          cout<<"You addeed Creamer to your Coffee\n"<<endl;

                          cost=pCoffee + pCreamerC;

                         break;

                        case ('3'):

                            cout<<"You added Ice and Creamer to your Coffee\n"<<endl;

                            cost= pCoffee + pIceC + pCreamerC;

                            break;

                        case ('4'):

                            cout<<"You added Ice blended\n"<<endl;

                            cost=pCoffee + pIceBC;

                            break;

                        default:

                            cout<<"You made no choice\n"<<endl;

                  }

        break;
                 //

                 toPay+= cost;
                 cost=0.0;

                 //

                 case ('D'):

                    cout<<"You are done with order\n"<<endl;

                    break;

                default:

                   cout<<"The letter you entered is not an option in the list!"<<endl;



                    }

           cout<<"Would you like any other order\n"<<endl;

        }

         while(choice!='D');



         cout<<"\n *****Thank you for visiting us *****"<<endl;

         cout<<"\nYour amount is:>>>"<<(toPay+=(toPay*0.05))<<"<<<"<<endl;

         cout<<"\n***********************************************"<<endl;


        return 0;   


}

New code is:

#include <iostream>
using namespace std;

int main ()
{


    int extrasC, extrasT;

    float pCoffee=1.50, pIceC=0.30, pCreamerC=0.50, pIceBC=1.20;

    float pTea=1.20, pIceT=0.20, pCreamerT=0.30, pIceBT=1.00;

    float cost=0.0, toPay=0.0;

    char choice;



    do

    {
        cout<< "Hello, please enter the lettet for your choice!" <<endl;

        cout<< "C Coffee"<<endl;

        cout<<"T Tea"<<endl;

        cout<<"D You are done with order"<<endl;

        cin>>choice;

        switch(choice)
        {

        //

        case ('T'):
            cout<<"Please choose your extras with your Tea:"<<endl;
            cout<<"0 NO extras"<<endl;
            cout<<"1 Only ICE"<<endl;
            cout<<"2 Only CREAMER"<<endl;
            cout<<"3 ICE & CREAMER"<<endl;
            cout<<"4 ICE blended"<<endl;

            cin>>extrasT;

            switch(extrasT)
            {
                case ('0'):

                     cout<<"You don't want any extras\n"<<endl;

                     cost=pTea;

                    break;

                case ('1'):

                      cout<<"We will add ice to your tea\n"<<endl;

                      cost=pTea+ pIceT;

                      break;

                 case ('2'):

                      cout<<"You added Creamer to your tea\n"<<endl;

                      cost=pTea+pCreamerT;

                      break;

                 case ('3'):

                      cout<<"You added Ice and Creamer to your tea\n"<<endl;

                      cost=pTea+ pIceT+ pCreamerT;

                      break;

                  case ('4'):

                       cout<<"You added Ice blended\n"<<endl;

                       cost= pCoffee + pIceBT;

                       break;

                   default:

                      cout<<"You made no choice\n"<<endl;

                  }

         break;

                //

                case ('C'):

                  cout<<"Please choose your extras with your Coffee:"<<endl;

                  cout<<"0 NO extras"<<endl;

                  cout<<"1 Only ICE"<<endl;

                  cout<<"2 Only CREAMER"<<endl;

                  cout<<"3 ICE & CREAMER"<<endl; 

                  cout<<"$ ICE blended"<<endl;

                  cin>> extrasC;

                  switch(extrasC)
                  {
                    case ('0'):

                        cout<<"You don't want any extras\n"<<endl;

                        cost= pCoffee;

                        break;

                      case ('1'):

                          cout<<"We will add Ice to your Coffee\n"<<endl;

                          cost= pCoffee + pIceC;

                          break;

                       case ('2'):

                          cout<<"You addeed Creamer to your Coffee\n"<<endl;

                          cost=pCoffee + pCreamerC;

                         break;

                        case ('3'):

                            cout<<"You added Ice and Creamer to your Coffee\n"<<endl;

                            cost= pCoffee + pIceC + pCreamerC;

                            break;

                        case ('4'):

                            cout<<"You added Ice blended\n"<<endl;

                            cost=pCoffee + pIceBC;

                            break;

                        default:

                            cout<<"You made no choice\n"<<endl;

                  }

        break;
                 //

                 toPay+= cost;
                 cost=0.0;

                 //

                 case ('D'):

                    cout<<"You are done with order\n"<<endl;

                    break;

                default:

                   cout<<"The letter you entered is not an option in the list!"<<endl;



                    }

           cout<<"Would you like any other order\n"<<endl;

        }

         while(choice!='D');



         cout<<"\n *****Thank you for visiting us *****"<<endl;

         cout<<"\nYour amount is:>>>"<<(toPay+=(toPay*0.05))<<"<<<"<<endl;

         cout<<"\n***********************************************"<<endl;


        return 0;   


}

So first thing I see is that you don't add anything to your toPay variable when you select "Tea" and when you select "Coffee" you break out of your switch statement too early and it doesn't execute the toPay += cost; code.

Also, your input from the choices on your keyboard go into 'int' variables. Then you compare them to a character. This won't work. Simply make your case arguments for your extras selections numbers, not the number character. (Or change the delcaration from int to char)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.