I'm making a menu program and wanted to check whether it is using global variables or not.
#include <iostream>
using namespace std;
int menu(int choice);
int balance(double & amount);
double deposit(double & amount);
double inventory(int & widgetb, int & widgetr);
double purchase(int & widgetb, int & widgetr, double & amount);
void leave();
int main()
{
int choice(0), widgetb(20), widgetr(20);
double amount = (0.00);
cout << "Welcome to the Widget Store!\n\n\n";
do
{
// print the menu, then get the user's choice
menu(choice);
cin >> choice;
// use the user's choice to select what to do next
// note that buyItem may update its first argument
if (choice == 1)
balance(amount);
else if (choice == 2)
deposit(amount);
else if (choice == 3)
inventory (widgetb, widgetr);
else if (choice == 4)
purchase (widgetb, widgetr, amount);
else if (choice == 5)
void leave();
else
cout << "Invalid choice, try again!\n";
} while (choice !=5);
return 0;
}
int menu(int choice)
{
cout << "What would you like to do:\n\n";
cout << "1) Check my account balance\n";
cout << "2) Add money to my account\n";
cout << "3) View store inventory\n";
cout << "4) Buy Widgets\n";
cout << "5) Leave store\n";
}
int balance(double & amount)
{
cout << "Your account balance is: $" << amount << "\n";
}
double deposit(double & amount)
{
double addition(0.0);
do
{
if(addition < 0)
{
cout << "Enter only positive numbers please!";
}else
cout << "How much money would you like to add to your account: ";
cin >> addition;
}
while(addition <= 0);
amount += addition;
cout << "your current balance is: " << amount << "\n";
}
double inventory(int & widgetb, int & widgetr)
{
cout << "The store has " << widgetb << " blue Widgets and " << widgetr << " red Widgets \n";
}
double purchase(int & widgetb, int & widgetr, double & amount)
{
int color(0), red(0), blue(0);
do
{
if(amount < red*5 + blue*5)
{
cout << "You dont have enough money for that \n";
}
if(red < 0 || blue < 0)
{
cout << "Enter only positive numbers for your purchase please! \n";
}
cout << "What color Widget would you like to buy?\n";
cout << "1) Red\n" << "2) Blue\n";
cin >> color;
if(color == 1)
{
cout << "How many red Widgets would you like to buy?\n";
cin >> red;
}else
{
cout << "How many blue Widgets would you like to buy?\n";
cin >> blue;
}
}while(amount < red*5 + blue*5 || red < 0 || blue < 0);
cout << "Thank you for your purchase! \n";
widgetr -= red;
widgetb -= blue;
amount = amount - ((5*red) + (5*blue));
}
void leave()
{
cout << "Fine then, we don't want your business anyway!\n";
}