Problem with Loop

Hello,

I need my code to be able to give money back to user when having paid more than the cost of the drink. Else, if they pay in full it's supposed to not ask for more money. Can you please help? It keeps asking for money even when paid in full. I can't paste the whole code. It is really long.

Here is the full code.

https://pastebin.com/hYEVPTui

https://drive.google.com/file/d/0B8Lw68S_gIqEb3A5aTdyczNmdjlsdnJSTUdZNTN1bkYxYzlN/view?usp=sharing


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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
 #include <iostream>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

const int MAX_NUM_DRINKS = 4;
const double DOLLAR = 1.00; // amounts in $.cc
const double QUARTER = 0.25;
const double DIME = 0.10;
const double NICKEL = 0.05;


//Function Prototypes
int displayDrinksAndGetUserChoice(const string[], const double[], const int[], int);
int displayOPerationsAndGetUserChoice();
void updateBeverageQty(int, int[]);
void updateSoldBeverageQty(int, int[]);
void ComputeTotalProfit(const double[], int[], double[], const int);
void DisplayTotalProfit(const string[], const double[], const int[], double[], const int);
int getBestPerformer(const double[], const int);
int getWorstPerformer(const double[], const int);
void displayBestDrink(const string[], const double[], const int[], double[], int);
void displayWorstDrink(const string[], const double[], const int[], double[], int);
bool isValidMoney(double);
double moneyHandler(double, double);

int main()
{
	int operation;
	int index = 0;
	bool valid = false;
	double change = 0.0;
	double coin = 0.0;


	string beverage[MAX_NUM_DRINKS] = { "Coca-Cola", "Sprite" , "Gatorade" , "Spring-Water" };
	double price[MAX_NUM_DRINKS] = { /* Coca-Cola*/1.50, /*Sprite*/1.25, /*Gatorade*/2.25, /*Spring-Water*/ 1.85 };
	int qtyOnHand[MAX_NUM_DRINKS] = {/* Coca-Cola*/20, /*Sprite*/20, /*Gatorade*/20, /*Spring-Water*/20 };
	int qtySold[MAX_NUM_DRINKS] = { 0,0,0,0 };
	double totalProfit[MAX_NUM_DRINKS];

	int selectedDrink;
	char userAnswer;

	do
	{
		selectedDrink = displayDrinksAndGetUserChoice(beverage, price, qtyOnHand, MAX_NUM_DRINKS);

		switch (selectedDrink)
		{
		case 1:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			cout << "Enter $1.50 Dollars to purchase a " << beverage[selectedDrink - 1] << endl;
			change = moneyHandler(price[selectedDrink - 1], coin);
			cout << "Dispensed change " << change << endl;
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			updateSoldBeverageQty(selectedDrink - 1, qtySold);
			break;

		case 2:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			cout << "Enter $ 1.25 Dollars to purchase a " << beverage[selectedDrink - 1] << endl;
			//cin >> coin;
			//moneyHandler(good, price[selectedDrink - 1], coin);
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			updateSoldBeverageQty(selectedDrink - 1, qtySold);
			break;

		case 3:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			cout << "Enter $ 2.25 Dollars to purchase a " << beverage[selectedDrink - 1] << endl;
			//cin >> coin;
			//moneyHandler(good, price[selectedDrink - 1], coin);
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			updateSoldBeverageQty(selectedDrink - 1, qtySold);
			break;

		case 4:
			cout << "You selected: " << beverage[selectedDrink - 1] << endl;
			cout << "Enter $ 1.85 Dollars to purchase a " << beverage[selectedDrink - 1] << endl;
			//cin >> coin;
			//moneyHandler(good, price[selectedDrink - 1], coin);
			updateBeverageQty(selectedDrink - 1, qtyOnHand);
			updateSoldBeverageQty(selectedDrink - 1, qtySold);
			break;
		default:
			cout << "Please enter 1,2,3 or 4 only!" << endl;
		}
		cout << "Do you want to go again? (Y/n):";
		cin >> userAnswer;

	} while ((userAnswer == 'Y') || (userAnswer == 'y'));

	do
	{

		operation = displayOPerationsAndGetUserChoice();
		switch (operation)
		{
		case 1:
			//calculate total profit
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
			break;
		case 2:
			//Best
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			index = getBestPerformer(totalProfit, MAX_NUM_DRINKS);
			displayBestDrink(beverage, price, qtySold, totalProfit, index);
			break;
		case 3:
			//worst
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			index = getWorstPerformer(totalProfit, MAX_NUM_DRINKS);
			displayWorstDrink(beverage, price, qtySold, totalProfit, index);
			break;
		case 4:
			//Totals
			ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
			DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
			break;
		case 5:
		default:
			cout << "For operations please enter 1, 2, 3, or 4 only, key in 5 to exit" << endl;
		}
	} while ((operation < 1) && (operation >= 5));

	system("PAUSE");
	return 0;
}

//Function Money handler
double moneyHandler(double price, double coin)
{
	// valid is first coin input checked
	bool checking = false; //second coin input
	double total_coin = 0.0;
	double change = 0.0;
	double addtional = 0.0;
	bool cont = false;
	bool valid = false;

	do
	{
		cin >> coin;
		valid = isValidMoney(coin);
		cout << "taken " << valid << endl;
		//total_coin += coin;
		//change = static_cast <double> (price - total_coin);

		if ((valid == true) && (total_coin < price) && ((coin == NICKEL) || (coin == DIME) || (coin == QUARTER) || (coin == DOLLAR)))
		{
			addtional = coin;
			total_coin = total_coin + addtional;
			coin = 0.0;
			cout << addtional << " Dollars accepted" << endl;
			addtional = 0;
			cout << "You have added a total of " << total_coin << " Dollars" << endl;
			change = static_cast <double> (price - total_coin);
			cout << "You will still need to add " << change << " Dollars" << endl;
			cout << "Please add more valid coins to pay for your drink" << endl;
			cin >> coin;
			checking = isValidMoney(coin);
			cont = true;
			if ((checking == true) && (cont == true) && (total_coin < price) && ((coin == NICKEL) || (coin == DIME) || (coin == QUARTER) || (coin == DOLLAR)))
			{

Last edited on
Consider:
debt = ...
WHILE ( 0 < debt )
  ask for money to cover 'debt'
  payment = ...
  IF ( debt <= payment )
    payment -= debt
    debt = 0
  ELSE
    debt -= payment
    payment = 0

IF ( 0 < payment )
  return change


Furthermore, use integer money. Floats are not precise. Store money in integer cents.
Convert to "decimal" just for show. For example:
1
2
int money = ... // cents
std::cout << '$' << money/100 << '.' << std::setfill('0') << std::setw(2) << money%100;
Topic archived. No new replies allowed.