Vending Machine Code Updated
- Keshav Batra
- May 5, 2023
- 2 min read
So now I've added some new features to this vending machine. Every time i try and go above the amount of drinks that are list, it will repeat the question and keep doing that unless I choose one of the following 5. Also when my player runs out of money, it will say insufficient funds.
class Drinks { // The class
public: // Access specifier
int DrinkID; // Attribute (int variable)
string DrinkName; // Attribute (string variable)
float DrinkPrice;
};
int main()
{
#pragma region Drink Variables
//Drink Object Variables
Drinks Water;
Water.DrinkID = 1;
Water.DrinkName = "Dasani";
Water.DrinkPrice = 5;
Drinks CokeZero;
CokeZero.DrinkID = 2;
CokeZero.DrinkName = "CokeZero";
CokeZero.DrinkPrice = 3;
Drinks Pepsi;
Pepsi.DrinkID = 3;
Pepsi.DrinkName = "Pepsi";
Pepsi.DrinkPrice = 3;
Drinks Sunkist;
Sunkist.DrinkID = 4;
Sunkist.DrinkName = "Sunkist";
Sunkist.DrinkPrice = 3;
Drinks Sprite;
Sprite.DrinkID = 5;
Sprite.DrinkName = "Sprite";
Sprite.DrinkPrice = 3;
#pragma endregion
//Player Variables
int PlayerWallet = 20;
int PlayerSelect;
int PlayerAnswer;
Drinks DrinkArray[5] = { Water, CokeZero, Pepsi, Sunkist, Sprite };
do
{
cout << "Welcome to TC Vending\n\n\n";
for (int i = 0; i < 5; i++)
{
cout << DrinkArray[i].DrinkID << ". " << DrinkArray[i].DrinkName << " - $" << DrinkArray[i].DrinkPrice << "\n"; //Displays the name of the drink, the price, and the number order
}
cin >> PlayerSelect;
for (int i = 0; i < 5; i++)
{
if (PlayerSelect == DrinkArray[i].DrinkID) //Allows the player to select the name of the drink that they choose
{
cout << "Is this your selection?";
cin >> PlayerAnswer;
if (PlayerAnswer == 1)
{
if (PlayerWallet < DrinkArray[i].DrinkPrice) //If the amount in the players wallet is less than the drink price
{
cout << "Insufficient Funds\n\n";
}
else
{
PlayerWallet -= DrinkArray[i].DrinkPrice; //Subtracts the amount of money from the players wallet
}
}
else
{
if (PlayerAnswer < DrinkArray[i].DrinkID)
{
cout << "Would you like to stop your selection?";
}
}
}
}
cout << "Current Funds:" << PlayerWallet<< "\n\n"; //Displays the current amount of money the player has
} while (PlayerWallet >= 0); //Will loop as long as the player has money and as long as the amount is greater than 0
system("pause");
}
コメント