Vending machine program

Hi :)
Just started learning C++ about a week ago and I attempted a vending machine program exercise with a pricing/change function as well.

http://codepad.org/H9GcWjwW

It seems to run fine with no bugs, but was wondering if any ''C++ savvy'' coders could take a look at it and see if it has the correct coding practices and such?

<3
1) avoid using directive. http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

2) Prefer to declare variables when you need them and initialize them with meaningful value immideately.

3) Most of the early returns are not needed, you can just let execution to fall through.

4) Checking if money are greater or equal to1 is unnesesary. Everything other will be caught by previous loop.

Here is slightly changed program: http://coliru.stacked-crooked.com/a/b3206bc59d3da208


Later, when you will learn more C++ you will see how repetitive this code is. You wrote "Here is your.." 5 times. And what if there were 100 beverages? 1000?

So later you will write code like this (Behaves just like yours. Not beautified) http://coliru.stacked-crooked.com/a/5b45b41c9ab7c61b
Sorry for the late reply, I thought nobody would respond to this!

Your advice is muchly appreciated!

thank you

edit: the way you wrote the program amazes me, and I learned a tonne from it, thank you!
Last edited on
Topic archived. No new replies allowed.