Vending Machine code while loop not working

So i wanted to ask why my while loop on line 103 of my code is not working when choice six is entered, i want the code to see 6 being entered and out put the money made so far but when 6 is entered it just asks how much money i will be putting in it so any help will be appreciated. also this is programming challenge 13 of chapter eleven in the starting out with C++ from control structures. Again thank you and i will be going to work in like 30 min so if i don't answer back right now i will as soon as I get off.

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
99
100
101
102
103
104
105
106
107
  #include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>


using namespace std;



const int NUM_DRINKS = 6;



struct Drink
{
    string  name;           
    double  price;          
    int     num;            
};
Drink Beverage;


int GetChoice(Drink m[])
{

    int choice;

    cout << " Please pick a drink" << endl;
    cout << "  Drink Options:  Prices" << endl;
    cout << " 1. Cola       : .75cents " << endl;
    cout << " 2. Root Beer  : .75cents " << endl;
    cout << " 3. Lemon Lime : .75cents " << endl;
    cout << " 4. Grape Soda : .80cents " << endl;
    cout << " 5. Cream Soda : .80cents " << endl;
    cout << " 6. I'm Leaving BYE." << endl;


    cin >> choice;

    if (choice < 1 || choice > 6)
    {
        cout << " please enter a number 1-5. ";
        return choice;
    }
}


void Transaction(Drink m[], int choice, double &earnings)
{
    float moneyRecieved;
    float change;


    cout << " Please enter an amount no more then a dollar ";
    cin >> moneyRecieved;



    if (moneyRecieved < 0 || moneyRecieved > 1)
    {
        cout << " This vending machine only allows for change and bills up to a dollar and no less than 0 cents ";
    }

    if (moneyRecieved >= Beverage.price)
    {
        cout << "Thank You!! Your drink will be coming right out ";
    }
    {


        change = moneyRecieved - m[choice - 1].price;

        cout << "Here is your change " << change << " Thank you come agian ";

        earnings = moneyRecieved - change; 
}
}

int main()
{
    int     choice = 0;                 
    double  earnings = 0.0;                 
    Drink   machine[NUM_DRINKS] =                       
    {
        { "Cola     ", 0.75, 20 },
        { "Root Beer", 0.75, 20 },
        { "Lemon-Lime", 0.75, 20 },
        { "Grape Soda", 0.80, 20 },
        { "Cream Soda", 0.80, 20 }
    };



    cout << fixed << showpoint << setprecision(2);      


    do
    {
        choice = GetChoice(machine);                            
        if (choice != NUM_DRINKS)
            Transaction(machine, choice, earnings); 
} while (choice != NUM_DRINKS || choice == 6);

    cout << "Total money earned: $" << earnings << endl;
    return 0;
}
Last edited on
The problem is your GetChoice function if I enter number <1 or >6, then it will return this number and then when it checks do-while condition it will be true since loop exits only if choice!=NUM_DRINKS or choice!=6 (it should be choice!=6 not choice == 6, because if choice is 6 program needs to end). So you should change GetChoice function to return only numbers 1,2,3,4,5 and 6.
So here is fixed code, which is working, I suppose, the way it should:
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
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>


using namespace std;



const int NUM_DRINKS = 6;



struct Drink
{
    string  name;
    double  price;
    int     num;
};
Drink Beverage;


int GetChoice(Drink m[])
{

    int choice;
    cout << " Please pick a drink" << endl;
    while(choice<1||choice>6){
    cout << "  Drink Options:  Prices" << endl;
    cout << " 1. Cola       : .75cents " << endl;
    cout << " 2. Root Beer  : .75cents " << endl;
    cout << " 3. Lemon Lime : .75cents " << endl;
    cout << " 4. Grape Soda : .80cents " << endl;
    cout << " 5. Cream Soda : .80cents " << endl;
    cout << " 6. I'm Leaving BYE." << endl;


    cin >> choice;
    if (choice < 1 || choice > 6)
        cout << "Please enter a number 1-6. "<<endl;

    }
    return choice;

}


void Transaction(Drink m[], int choice, double &earnings)
{
    float moneyRecieved;
    float change;


    cout << " Please enter an amount no more then a dollar ";
    cin >> moneyRecieved;



    if (moneyRecieved < 0 || moneyRecieved > 1)
    {
        cout << " This vending machine only allows for change and bills up to a dollar and no less than 0 cents ";
    }

    if (moneyRecieved >= Beverage.price)
    {
        cout << "Thank You!! Your drink will be coming right out ";
    }
    {


        change = moneyRecieved - m[choice - 1].price;

        cout << "Here is your change " << change << " Thank you come agian ";

        earnings = moneyRecieved - change;
}
}

int main()
{
    int     choice = 0;
    double  earnings = 0.0;
    Drink   machine[NUM_DRINKS] =
    {
        { "Cola     ", 0.75, 20 },
        { "Root Beer", 0.75, 20 },
        { "Lemon-Lime", 0.75, 20 },
        { "Grape Soda", 0.80, 20 },
        { "Cream Soda", 0.80, 20 }
    };



    cout << fixed << showpoint << setprecision(2);


    do
    {
        choice = GetChoice(machine);
        if (choice != NUM_DRINKS)
            Transaction(machine, choice, earnings);
} while (choice != NUM_DRINKS || choice != 6);

    cout << "Total money earned: $" << earnings << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.