Calculation Problem

closed account (E85L3TCk)
Suppose you own a soft drink distributorship that sells Coca-Cola, Pepsi, Sprite, and Fanta. You are required to write a menu-driven program to handle the transactions done. The menu should display the following options:
1 – Order Soda
2 – Make Payment
3 – Quit
If the user chooses option 1, the program should display the list of soda available together with the corresponding prices. The user may choose more than one soda to buy. For each soda, request a quantity to buy. Display the running total after each soda has been chosen and prompt the user whether to buy more soda or finish ordering. If no more ordering is made, the program should display the menu again. If the user chooses option 2, display the total to be paid, prompt for the amount to be given by the user, display the balance need to be returned back to the user, and re-initialize all variables to zero. If no order has been made when this option is chosen, the program should display a message saying “No order made”. Option 3 should allow the program to terminate.

Below is my code but I do not know what's wrong with my calculation, it didn't display the correct drink total and total. I hope to get some help.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
    float total=0;
    float drinktotal,price,quantity;
    int selection,selectiondrink,paid,balance;
    char addsoda;
    bool done =false;

   do
   {
    //prompt the user to select
do
{
    cout<<"1-Order Soda"<<endl;
    cout<<"2-Make Payment"<<endl;
    cout<<"3-Quit"<<endl;
    cout<<"Please select a number to proceed to next step:";
    cin>>selection;
    system("clear");
    //selection
    switch (selection)
    {
    case 1:
    do{
    cout<<"1.Coca-cola=RM3.00"<<endl;
    cout<<"2.Pepsi=RM2.00"<<endl;
    cout<<"3.Sprite=RM4.00"<<endl;
    cout<<"4.Fanta=RM5.00"<<endl;
    cout<<setw(10)<<endl;

    for (int x=1;x<=1;x++)
        {
        cout<<"Select a number :";
        cin>>selectiondrink;
        if (selectiondrink=='1')
            price=3.00;

        else if (selectiondrink=='2')
            price=2.00;

        else if(selectiondrink=='3')
            price=4.00;

        else if(selectiondrink=='4')
            price=5.00;

        cout<<"Please enter the quantity:";
        cin>>quantity;
       drinktotal = price * quantity;

        cout<<setw(10)<<endl;
        cout<<"Your total is:RM"<<drinktotal<<endl;


        }
    cout<<setw(10)<<endl;
    cout<<"Your total for all drinks is:RM"<<total<<endl;
    cout<<"Order more soda?(y/n)";
    cin>>addsoda;
    }while (addsoda=='y');
    done =true;
    break;

   case 2:

    if(! done )
   {
   cout<<"No order made"<<endl;
   }
   else
   {
    total += drinktotal;
   cout<<"Your amount is:"<<total<<endl;
   cout<<"Total paid is:RM";
   cin>>paid;
   balance=paid-total;
   cout<<"Your balance is:RM"<<balance<<endl;
   }
   break;


   case 3:

   cout<<"The program is terminating..."<<endl;
   return 0;
}

    }while (! done);
    }while( addsoda=='n');

    return 0;
}

Thank you.
Last edited on
line 10: price is an uninitialized (garbage) variable.

line 11: selectiondrink is an int.

lines 41,44,47,50: You're treating selectiondrink as a char. Consequently none of your if statements evaluate true and price is never set.

line 55: You're trying to perform a calculation with an uninitialized variable.

line 37: What is the purpose of this loop? It will execute exactly once.

BTW, you need to break your programs into functions to make it easier to follow.

Last edited on
Topic archived. No new replies allowed.