Cans Machine C++ Programming

Pages: 12
How do I created a loop for this codings?

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
#include <iostream>
using namespace std;

int quantity = 0;
int selection = 0;
float amount = 0;
float total = 0;


int main();

}
int main()
{
	{
		cout << "\n Please select the below shown item <1-3>" << endl;
		cout << " 1. Coco Cola  ($1.00 / Available: " << currentAmountCoke << ")" << endl;
		cout << " 2. Pepsi Cola ($1.00 / Available: " << currentAmountPepsi << ")" << endl;
		cout << " 3. Sprite     ($1.00 / Available: " << currentAmountSprite << ")" << endl;
		cout << "\n Choice: ";
		cin >> selection;

			cout << "\n Please select the quantity: ";
			cin >> quantity;

				}
				else
				{
					cout << "\n Insufficient can of drink, please try another choice \n";
					quantity = 0;
				
				}
			}
			
					
					cout << "\n Insufficient can of drink, please try another choice";
			
					
		
	
				else
				{
					cout << "\n Insufficient can of drink, please try another choice";
					quantity = 0;
					
				}
			}
		}
		else
		{
			cout << "\n Incorrect Selection, please enter 1 to 3";
			
		}
	}

	cout << "\n Currently out of drinks";
	cin.ignore();
	cin.ignore();

}
Last edited on
Any helpers?
To start with, rather than your hard-coded '10' on line 19, you'll need separate variables to keep track of the current quantity of each type of drink.

1
2
3
int currentAmountCoke = 10;
int currentAmountPepsi = 10;
int currentAmountSprite = 10;


and when a selection is made decrement the appropriate one.
e.g.
 
currentAmountCoke -= quantity;
Last edited on
Thanks mutexe, How do I do the looping?
Last edited on
well the whole thing should be a loop. And if when you try and decrement and get minus one you need to report the drink has gone.
Last edited on
Hey

You can make a simple while-loop

Start here:

1
2
3
4
5
6
while ((currentAmountCoke <= 10) || (currentAmountPepsi <= 10) || (currentAmountSprite <= 10)) {
   cout << "Please select the below shown item <1-3>" << endl;
   ...
      quantity=0;
   }
}


I would also show how many drinks are remaining!

ANproCUBE
Hi Thanks ANproCUBE,

After i'm done the loop how do I continue?

Sorry I am a beginner. I may need someone who is experience to help me with it?
Hey

I can see that you have a few problems to use our suggestions. So I wrote this code for you today. All you have to to is to read this code and try to understand what I did.

If you still have questions about the code, please ask!

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
//-----------------------------------------------
// Things you can add:
// + Defines for different prices for each drink
//   -> You would have to calculate
//      each total individually
//      with its price
//      e.g. "#define COKE_PRICE 1.20"
// + More drinks
//   -> For example drinks with alcohol where
//      you would have to ask for the age...
// + Save the stats to a external file to keep
//   the amount of drinks in the machine
//-----------------------------------------------

//Includes
#include <iostream>
#include <conio.h> //You only have to write "getch();", but I'm using VS 2013

//Defines
#define QUANTITY_MAX 10

//Namespaces
using namespace std;

//Global Variables
int quantity = 0;
int selection = 0;
int currentAmountCoke = QUANTITY_MAX;
int currentAmountPepsi = QUANTITY_MAX;
int currentAmountSprite = QUANTITY_MAX;
float amount = 0;
float total = 0;


int main();
void wait()
{
	cout << "\n Press a key: ";
	fflush(stdin);
	_getch();
	main();
}
void vendingMachineRoutine()
{
	system("cls");
	total = 1.00 * quantity;
	cout << "\n Please insert " << total << "$: ";
	cin >> amount;

	if (amount > total)
	{
		system("cls");
		amount -= total;
		cout << "\n Please collect drink and change of " << amount << "$";

		if (selection == 1)
		{
			currentAmountCoke -= quantity;
		}
		else if (selection == 2)
		{
			currentAmountPepsi -= quantity;
		}
		else if (selection == 3)
		{
			currentAmountSprite -= quantity;
		}
		wait();
	}
	else if (amount == total)
	{
		system("cls");
		cout << "\n Please collect your drink";

		if (selection == 1)
		{
			currentAmountCoke -= quantity;
		}
		else if (selection == 2)
		{
			currentAmountPepsi -= quantity;
		}
		else if (selection == 3)
		{
			currentAmountSprite -= quantity;
		}
		wait();
	}
	else
	{
		system("cls");
		cout << "\n Amount is insufficient";
		wait();
	}
}
int main()
{
	while ((currentAmountCoke <= 10) || (currentAmountPepsi <= 10) || (currentAmountSprite <= 10))
	{
		system("cls");
		cout << "\n Please select the below shown item <1-3>" << endl;
		cout << " 1. Coco Cola  (1$ / Available: " << currentAmountCoke << ")" << endl;
		cout << " 2. Pepsi Cola (1$ / Available: " << currentAmountPepsi << ")" << endl;
		cout << " 3. Sprite     (1$ / Available: " << currentAmountSprite << ")" << endl;
		cout << "\n Choice: ";
		cin >> selection;

		if ((selection >= 1) && (selection <= 3))
		{
			system("cls");
			cout << "\n Please select the quantity: ";
			cin >> quantity;

			if (selection == 1)
			{
				if ((quantity <= QUANTITY_MAX) && (quantity <= currentAmountCoke) && (quantity > 0))
				{
					vendingMachineRoutine();
				}
				else
				{
					system("cls");
					cout << "\n Insufficient can of drink, please try another choice";
					quantity = 0;
					wait();
				}
			}
			else if (selection == 2)
			{
				if ((quantity <= QUANTITY_MAX) && (quantity <= currentAmountPepsi) && (quantity > 0))
				{
					vendingMachineRoutine();
				}
				else
				{
					system("cls");
					cout << "\n Insufficient can of drink, please try another choice";
					quantity = 0;
					wait();
				}
			}
			else if (selection == 3)
			{
				if ((quantity <= QUANTITY_MAX) && (quantity <= currentAmountSprite) && (quantity > 0))
				{
					vendingMachineRoutine();
				}
				else
				{
					system("cls");
					cout << "\n Insufficient can of drink, please try another choice";
					quantity = 0;
					wait();
				}
			}
		}
		else
		{
			system("cls");
			cout << "\n Insufficient Number";
			wait();
		}
	}

	system("cls");
	cout << "\n Currently out of drinks :(";
	wait();
}


I hope it's helpful,
ANproCUBE
Hey

I forgot to mention your question :D

After the loop you can do what you want...
You can, for example, refill the machine or something like that...

ANproCUBE
Line 98:
while ((currentAmountCoke <= 10) || (currentAmountPepsi <= 10) || (currentAmountSprite <= 10))
looks like infinite loop.
Maybe
while ((currentAmountCoke > 10) || (currentAmountPepsi > 10) || (currentAmountSprite > 10))
be better.
Only 168 lines for this code? Looser.
Try to ask "-1" amount of doses. =)
Hey

OK... I don't really understand what's the problem here ;)

When I ask for -1 it says "Insufficient can of drink, please [...]"

That's what the (quantity > 0) for :D

And please don't call me loser.
I tried to make a simple code (OK, it's a little bit longer than expected), but I think
this works pretty well.
And if there's a mistake, as you said, please give constructive feedback.

ANproCUBE
Hey

I looked over it again...
I found one new mistake and corrected the one that you found:

1) Your suggestion doesn't work either. It works with: while ((currentAmountCoke != 0) || (currentAmountPepsi != 0) || (currentAmountSprite != 0))

2) At the end I used my function "wait". Use getch() or _getch() instead

ANproCUBE
hey thanks ANproCUBE for your speedy reply. I manage to understand the coding somehow, although your coding are still a little advance to me. Thanks a lot.
hey ANproCUBE, appreciate if you able to help me on another code from this link.

http://www.cplusplus.com/forum/general/157753/
Hey

OK, I'll take a look.

Also consider to mark this thread as solved (but only if you have really understood all of the information) ;)

ANproCUBE
HI ANproCUBE,

I have solved the another thread. Thanks for your help.
Hey

When I said you should mark this as solved I didn't mean you to rename the whole thread :D

Just mark it because if somebody wants to know the exact same thing you wanted to know it's hard to find!

ANproCUBE
Hi ANproCUBE,

Need your help on this.

http://www.cplusplus.com/forum/general/157761/

ANproCUBE: I'm sorry for my words. You are right. It's all ok.
Pages: 12