Cans Machine C++ Programming

Pages: 12
Try this one. Only 71 lines.
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
#include <iostream>
#include <conio.h>

using namespace std;

int quantity = 0;
int currentAmountCoke = 10;
int currentAmountPepsi = 10;
int currentAmountSprite = 10;
float amount = 0;
float total = 0;

void wait()
{
	cout << "Press Enter\n";
	fflush(stdin);
	_getch();
}

int main()
{
	while (currentAmountCoke || currentAmountPepsi || currentAmountSprite)
	{
		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: ";

		int selection;
		cin >> selection;
		
		int* exist;
		switch (selection) {
		case 1: exist = &currentAmountCoke; break;
		case 2: exist = &currentAmountPepsi; break;
		case 3: exist = &currentAmountSprite; break;
		default:
			cout << "Wrong number\n";
			wait();
			continue;
		}

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

		if ((quantity <= *exist) && (quantity > 0)) {
			total = 1.00 * quantity;
			cout << "\n Please insert " << total << "$: ";
			cin >> amount;

			if (amount < total)
				cout << "Amount is insufficient\n";
			else {
				amount -= total;
				*exist -= quantity;
				if (amount)
					cout << "Please collect drink and change of " << amount << "$\n";
				else
					cout << "Please collect your drink\n";
			}
		} else
			cout << "Insufficient can of drink, please try another choice\n";
		wait();
	}

	system("cls");
	cout << "Currently out of drinks :(\n";
	wait();
}
ANproCUBE:
1. Why do you use define?
1
2
#define QUANTITY_MAX 10
int currentAmountCoke = QUANTITY_MAX;


2. Lines 38-40. cout << "\n Press a key: "; Do your really see this text without ending newline?

3. Line 41 of your program. Why do you call main(). It leads to bad reccursion.

4. Line 43. vendingMachineRoutine(). Why do you extract this part of your program into other function?

5. Lines 56-67 and other. Why do you repeat almost the same code too many times? It's easier to write it only once and mark differences.

6. Lines 116, 130, 144. (quantity <= currentAmountCoke) means (quantity <= QUANTITY_MAX). So only (quantity <= currentAmountCoke) is enough.

7. Why do you use this style?
1
2
3
4
if(some_condition)
{
 one_line_of_code;
}

More empty lines you have, less code you see on your screen. Why do you hide your code from yourself?
Hi Konstantin2,

I guess there are various method to come out with the solution. Thanks for giving another and faster solution to solve this issue. Appreciated it.
Hey

OK, let's answere some questions here :D

1) If you want to list all your values set for each variable, it's easier so read using defines.
And if you want to use the same value for more than one variable, changing it by simply changing one value is way better

2) I don't really know what you mean. With my compiler there isn't any difference between using "<endl;" at the end or not. The only thing is that it won't make a line brake.
(If I'm wrong, please correct me)

3) I don't think that recursions are a big deal, only the variables shouldn't be redefined...
That's why I put them into global. But that brings me to the next point ->

4) (This is my opinion!) I wanted to make the code as simple as possible. I don't want to give out a full, maxed out code to the questioner so he can just simply use it without thinking about the process of the code itself. Thats why I gave him some points to improve the code and to practice his coding (listed at the beginning of my code). I know that there are a lot of things you can improve but it's not necessary for the questioner (I guess), when he wants to learn new things.

(For example: You used a pointer in your version, but this maybe brings up new questions for less advanced people)

5) The same as (4)

6) You are totally right. It's not a mistake but it's unnecessary...

7) I learned it this way, but I don't think it's harder to read.
> If you want to add code you always have to add the missing brackets
> other code languages don't support this kind of "only one line of code" thing

If you want me to, I can remove them before I post the code on the forum.
I'm always open for new things! I'm not perfect in any way :D

I hope you can see my point of view more clear now,
ANproCUBE

PS: Sorry when there are a few mistakes in the text, but english is not my main language and I had to write this whole thing again because there was a failure when it tried to upload it...
Last edited on
Konstantin2 wrote:
7. Why do you use this style? [...] More empty lines you have, less code you see on your screen. Why do you hide your code from yourself?

So many times, I've seen bugs enter code, because people write:

1
2
if (condition)
  execute_a_single_command();


and then, later, they want to add more actions to the conditional block, but just add a line to the code and forget to add the braces, making:

1
2
3
if (condition)
  execute_a_single_command();
  do_something_else();

Putting the braces in straight away is good defensive practice. It makes it less likely you - or someone else - will introduce a bug later. I'd always advise people to do it. This isn't 1993, and we're not working on 13" VGA monitors any more. We have large, hi-def monitors that can show acres of code, so we can afford to lose a couple of lines from the display in pursuit of robustness and maintainability.

And there's no need to be snarky. If you want to know why someone did something, just ask them. Don't add snide comments - that's rude, and makes this forum a more unpleasant place for everyone.

ANproCUBE wrote:
3) I don't think that recursions are a big deal, only the variables shouldn't be redefined...

In general, there's nothing wrong with recursion. But calling main() from within your program is illegal in C++, and is undefined behaviour.

That's why I put them into global.

Global variables are a bad habit, and can lead to all sorts of problems. I'd advise you to learn better, safer ways of passing data around your program ASAP, rather than to carry on reinforcing bad habits.
Last edited on
Hey

Global variables are a bad habit, and can lead to all sorts of problems. I'd advise you to learn better, safer ways of passing data around your program ASAP, rather than to carry on reinforcing bad habits.


I totally agree with that. But as I mentioned: I wanted to make this code as simple as possible so I didn't want to pass all the values to the functions and so on...

Still a bad role model *hem*

ANproCUBE
MikeyBoy:
I like such style:
1
2
if (condition)
  execute_a_single_command();

Also I afraid that in nearest future this comact pretty style become deprecated.
When other people make mistakes, it's not my problem. They sould be more careful.
When other people wound theirseves by blade of shavers, it's not a reason to forbid shaving for everybody.
Sure, If you want, u can use braces for one line operators.

Don't add snide comments

I'm sorry. I was bad.

But calling main() from within your program is illegal in C++

Really. By 3.6.1.3. But I think that there is nothing bad if you use such reccursion carefully.

Global variables are a bad habit, and can lead to all sorts of problems.

Every variable can lead to problems if used brainlessly.
I know only one real reason to avoid global variables. In x64 processors accessing to global variable by absolute address is one tick longer than accessing local variable by relative address.
Topic archived. No new replies allowed.
Pages: 12