Good way to program?

Hey, I was doing the Cola Machine exercise by Blitz Coder and I just wanted to ask if this was a good way to do it or are there more efficient / better ways?

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
//Exercises for beginners
// *** COLA MACHINE ***

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int drinkChoice;
    double price[6] = {0, 2.50, 2.25, 2.25, 2.50, 2.00};
    string drinks[6] = {"0" , "Coca Cola", "Sprite", "Fanta", "Pepsi MAX", "Bottled Water"};

    cout << "Please insert 1 -5 for your preferred drink" << endl;
    cout << "1. Coca Cola $2.50" << endl;
    cout << "2. Sprite $2.25" << endl;
    cout << "3. Fanta $2.25" << endl;
    cout << "4. Pepsi MAX $2.50" << endl;
    cout << "5. Bottled Water $2.00\n" << endl;
    
    cin >> drinkChoice;

    if (drinkChoice >= 1 && drinkChoice <= 5)
    {
        cout << "Thank you for choosing " << drinks[drinkChoice] << " priced at $" << price[drinkChoice] << "." << endl;
    }
    else
    {
        cout << "Invalid choice. Money refunded." << endl;
    }

    system("PAUSE");
    return(0);
}

Last edited on
You started your question too broadly.
Is the last sentence a narrower version of the first, or the two are unrelated and you really want to know if there's "a good way to program"?

As for the switch, no. It's god-awful. All the cases do exactly the same.
Thanks helios.. never noticed they were ALL exactly the same.. xD I changed it so it should be better now.

I just wanted to ask if this was an efficient way to do this problem
Last edited on
Topic archived. No new replies allowed.