How can i improve this code? Cola Machine

So I was doing some exercise and cam across cola machine..

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
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

void MainMenu();
double Change(int&);
void UserOption(char);
int UserAmount(int);

int main()
{
	//Declaring
	int bill = 0;
	int userBalance = 0;
	char user_option;

	MainMenu();		// Shows main menu
	
	//Gets User Choice
	do
	{
		user_option = _getch();			// Gets user Input on keypressed
		UserOption(user_option);		// Displays the amount due
	}while(user_option > 5 && user_option < 1);


	//Calculates the Change
	userBalance = UserAmount(bill);		// Adds the bill to the userBalance
	if(userBalance < 1.75){
		cout << "Add more funds." << endl;
		while(userBalance < 1.75){
			cout << endl;
			userBalance += UserAmount(bill);
		}
	}
	cout << Change(userBalance);		// Calculates the Change due to user
	cout << endl;
	cout << "Thanks for Purchasing here!" << endl << endl;
	return 0;
}

void MainMenu(){
	cout << "Cola Machine" << endl << endl;
	cout << "---------------" << endl;
	cout << "1." << setw(11) << "Coca-Cola" << endl << 
		"2." << setw(8) << "sprite" << endl <<
		"3." << setw(13) << "Orange Coke" << endl <<
		"4." << setw(15) << "Vitamin Water" << endl <<
		"5." << setw(7) << "Water" << endl;
}

double Change(int& userBalance){
	int bill = 0;
	//If user tries to not input anything
	if(userBalance < 0){
		cout << "I'm sorry you must enter a valid bill." << endl;
		return -1;
	}
	//Calculates the Change due.
	else
		return userBalance - 1.75;
}

int UserAmount(int bill){
		cout << "Enter your bill amount: ";
		do{
			cin >> bill;
		}while(bill < 1);

		return bill; // Return bill, will be added to userBalance
}

void UserOption(char user_option){
		switch(user_option)
	{
	case '1':
		cout << "Coca-Cola -> $1.75" << endl;
		break;
	case '2':
		cout << "Sprite -> $1.75" << endl;
		break;
	case '3':
		cout << "Orange Coke -> $1.75" << endl;
		break;
	case '4':
		cout << "Vitamin Water -> $1.75" << endl;
		break;
	case '5':
		cout << "Water -> $1.75" << endl;
		break;
	default:
		cout << "Enter a valid option 1-5" << endl;
		break;
	}
}


This is my final version.... Any suggestion on what I could do to improve?
Any suggestion on what I could do to improve


- don't using namespace std: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
- const qualify your function parameters wherever feasible - in this case probably for all 3 that take arguments
- stylistically - proper indentation - suggest Allman - https://en.wikipedia.org/wiki/Indent_style
- don't use conio.h and subsequently _getch() - not part of the standard library - https://en.wikipedia.org/wiki/Conio.h
- also need proper input validation in case user enters incorrect data
- have a loop with exit condition in the switch so that user can order re-order or exit
Line 66: bill should be a local variable, NOT an argument.

Your cola machine handles only one price. Rather rigid restriction.

What about inventory? Does your cola machine have an unlimited supply of each drink?

Orange Coke? Bleah.

Since you're trying to learn C++, you should consider making your cola machine a class.

now that you mention class. any good article to learn classes...? all the ones I have read explain the same thing and i kinda don't understand classes in depth..
And Thanks for the help T_T
The tutorial on this site is very good:
http://www.cplusplus.com/doc/tutorial/classes/

Topic archived. No new replies allowed.