HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Drink machine using structs homework assignment

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
homeworkdrinkassignmentusingstructsmachine

Problem

Here's the school assignment I did:


Problem 1. Write a program that simulates a soft drink machine.


The program should use a structure Beverage that stores the following
data:



  • Drink Name



  • Drink Cost



  • Number of Drinks in Machine





The program should create an array of five structures. The elements
should be initialized with the following data:


$$\begin{array}{l|r|c}
\textrm{Drink Name} & \textrm{Cost} & \textrm{Number in Machine} \\
\hline
\textrm{Cola} & 0.75 & 20 \\
\textrm{Root Beer} & 0.75 & 20 \\
\textrm{Lemon-Lime} & 0.75 & 20 \\
\textrm{Grape Soda} & 0.80 & 20 \\
\textrm{Cream Soda} & 0.80 & 20 \\
\end{array}
$$


Each time the program runs, it should enter a loop that performs the
following steps:



  • A list of drinks is displayed on the screen.



  • The user should be allowed to either quit the program or pick a drink.



  • If the user selects the drink, he or she will next enter the amount of money that is to be inserted into the drink machine.



  • The program should display the amount of change that would be returned and subtract one from the number of that drink left in the


machine.

  • If the user selects a drink that had sold out, a message should be displayed. Similarly, if the user does not specify enough money, a


different message should be displayed (and the drink should not be
dispensed).



When the user chooses to quit the program, it should display the total
amount of money the machine earned.

And here's my code:

```
#include
#include
#include
#include
struct Beverage {
std::string drinkName;
const double drinkCost;
unsigned int numberOfDrinks;

};
struct DrinkMachine {
Beverage * drinks;
double revenue;
};
void initialDrinkMachinePrompt(Beverage drinks[], int numOfDrinks);
void promptDrinkMachineMenu(int numOfDrinks);
char getDrinkMachineMenuResponse(int numOfDrinks);
bool isValidDrink(char response, int numOfDrin

Solution

Disclaimer: I'm primarily a C# developer

No using namespace std; get's a huge thumbs up!

Your code looks like C written in C++.

Stop using raw arrays and start using container types such as std::array, std::vector and so on.

Use references over pointers when possible. Make them constant references when you don't need to modify it.

void displayDrinks(Beverage drinks[], int numOfDrinks);


becomes

void displayDrinks(std::vector const& drinks);


and you can use drinks.size() instead of passing along an extra parameter.

If you need to keep a pointer to an object you own use std::unique_ptr (std::make_unique) and std::shared_ptr (std::make_shared) if ownership is shared. Plain pointers is only for when you don't own the object and someone else is responsible for deallocating it. Now you won't ever have to call new and delete.

Use size_t for iterators and indexers and int32_t, uint32_t, int64_t, uint64_t when you care about sizes and signedness. Plain int is not guaranteed to be 32bit. Remember signed overflow is undefined.

Always use std::string for string manipulation and comparisons. char and char* can be hard to handle correctly.

Start encapsulating logic in classes when you learn about them. Use both header files and implementation files.

Code Snippets

void displayDrinks(Beverage drinks[], int numOfDrinks);
void displayDrinks(std::vector<Beverage> const& drinks);

Context

StackExchange Code Review Q#138496, answer score: 2

Revisions (0)

No revisions yet.