How can i make Code better?

Hello I created source code for a coca cola machine, please give me feedback on how I can make my code better. I want to improve by taking critical feedback. Please help. Thank you.

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
/*Requires:
 variables, data types, and numerical operators
 basic input/output
 logic (if statements, switch statements)

 Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever).
 Then allow the user to choose a beverage by entering a number 1-5.
 Output which beverage they chose.

 ★ If you program uses if statements instead of a switch statement, modify it to use a switch statement.
 If instead your program uses a switch statement, modify it to use if/else-if statements.

 ★★ Modify the program so that if the user enters a choice other than 1-5 then it will output "Error. choice was not valid, here is your money back."

*/
#include <iostream>
#include <conio.h>
using namespace std;
int beverage;   //gives user ability to choose which beverage they want
int coke=1; //type of beverage
double cokeprice= 1.25; //cost of  coke beverage
int water=2;    //type of beverage
double waterprice= 0.75;    //cost of water
int sprite=3;   //type of beverage
double spriteprice= 1.25;   //cost of sprite
int fanta=4;    //type of beverage
double fantaprice= 1.25;    //cost of beverage
int powerade=5; //type of beverage
double poweradeprice= 1.5;  //cost of beverage
double total; //used for telling user how much he/she owes the machine
int amount; //used for asking how much of the specific beverage they want
int main()
{
cout<<"Hello user choose any number of the five beverages listed below (1-5).\n\n"<<endl;

cout<<"1.Coke, price $1.25"<<endl;
cout<<"2.Water, price $0.75"<<endl;
cout<<"3.Sprite, price $1.25"<<endl;
cout<<"4.Fanta, price $1.25"<<endl;
cout<<"5.Powerade, price $1.50"<<endl;

cin>>beverage;

switch (beverage)
{
case 1:
    {
        cout<<"Thank you for choosing coke, how many would you like?"<<endl;
        cin>>amount;
        total=cokeprice*amount;
        cout<<"The amount due is $"<<total<<"."<<endl;
        break;
    }
case 2:
    {
        cout<<"Thank you for choosing water, how many would you like?"<<endl;
        cin>>amount;
        total=waterprice*amount;
        cout<<"The amount due is $"<<total<<"."<<endl;
        break;
    }
case 3:
    {
        cout<<"Thank you for choosing sprite, how many would you like?"<<endl;
        cin>>amount;
        total=spriteprice*amount;
        cout<<"The amount due is $"<<total<<"."<<endl;
        break;
    }
case 4:
    {
        cout<<"Thank you for choosing fanta, how many would you like?"<<endl;
        cin>>amount;
        total=fantaprice*amount;
        cout<<"The amount due is $"<<total<<"."<<endl;
        break;
    }
case 5:
    {
        cout<<"Thank you for choosing powerade, how many would you like?"<<endl;
        cin>>amount;
        total=poweradeprice*amount;
        cout<<"The amount due is $"<<total<<"."<<endl;
        break;
    }
default:
    {
        cout<<"Error. choice was not valid, here is your money back."<<endl;
    }
}


getch();
return 0;
}
I would change variable names to accurately reflect what they are trying to represent:
int coke[/coke] should be [code]int cokeID or something like that. The integer isn't the coke itself, it's an identification number that represents the selection of a coke. Similarly, int amount could be int beverageamount, because it's the total number of beverages. Being more specific with variables will help you when (inevitably) your code gets more complicated.

I think you could implement a beverage as a struct or a class, something like:

1
2
3
4
5
6
struct Beverage
{
     int ID;
     std::string name;
     float price;
}


Then in your main function you could create all the Beverages:

1
2
3
4
int main()
{
     Beverage coke, sprite, fanta; //etc.
}


Lastly, you can have a single function called void chooseDrink(int ID_); that will handle the ordering decisions, rather than a switch with 6 different cases. For your vending machine the switch isn't so bad because there are only 6 cases, but imagine a Coke freestyle (or something like that), which could have 100 drink options. This would lead to an enormous, intractable block of code.

Last edited on
@hyperfine

I could be more specific when naming my variables like you said, but where can I go to learn more of struct class .

It seems like structure/class would make my programming a lot smoother, and could lessen time.

Thanks for your reply m8 :D
I could be more specific when naming my variables like you said, but where can I go to learn more of struct class .


Google (unless you're in China). I would just Google "class c++", read the webpage, and try to implement a beverage class into your program. You can come back and I'll look at it, but it should be pretty straight forward.

Good luck!
Alternatively, here's a tutorial:
http://www.cplusplus.com/doc/tutorial/structures/

Best of luck. Also, you may want to make your beverage objects const if you go that route, since you won't be changing any of their data, yes?

-Albatross
ok thanks I will google about the classes for c++

And make the variables that I don't want to change, constant.

Thanks for the help!
Topic archived. No new replies allowed.