I have attached my code, and in the void quantityLogic function have left a simple way I have attempted to change the quantitiy of the first option, coca-cola. I have tried other ways such as creating temp variables and arrays, and swapping. Placing the -- in different parts. It seems to work, but once it returns back into the to subtract another drink, it restarts at 20. I understand that arrays are basically references, so the value should be stored automatically if im not mistaken. The idea is to subtract a drink from the drink[].quantity variable every time the user chooses a drink.
const int NUM_ELEMENTS = 5;
const int SIZE = 20;
// Strucutre containing variables for drink information
struct DrinkInfo
{
char name[SIZE];
double price;
int quantity;
};
void getFileData(DrinkInfo [], int); // Function prototype to retrieve data from drinks.txt file
int userSelectionMenu(); // Function prototype to display menu.
double getUserCash(DrinkInfo []); // Function prototype for user to enter cash
double getChange(DrinkInfo [], int, double); // Function to return change to user.
void getTotalIncome(DrinkInfo [], int, float&); // Function to obtaim total income at end of user session
void quantityLogic(DrinkInfo [], int);
void clear();
int main(int argc, char *argv[])
{
int userChoice; // Holds user drink selection
float totalIncome = 0; // Varibale initiated to hold total income
double cashEntered; // Variable to function getUserLogicCash
DrinkInfo drink[NUM_ELEMENTS]; // Structure decleration for drink information variables
cout << setprecision(2) << fixed;
/*
Loop that verifies userinput and calls functions
*/
do
{
getFileData(drink, NUM_ELEMENTS);
userChoice = userSelectionMenu();
switch(userChoice)
{
case 1: quantityLogic(drink, userChoice);
cashEntered = getUserCash(drink);
getChange(drink, userChoice, cashEntered);
getTotalIncome(drink, userChoice, totalIncome);
clear();
break;
case 2: cashEntered = getUserCash(drink);
getChange(drink, userChoice, cashEntered);
getTotalIncome(drink, userChoice, totalIncome);
clear();
break;
case 3: cashEntered = getUserCash(drink);
getChange(drink, userChoice, cashEntered);
getTotalIncome(drink, userChoice, totalIncome);
clear();
break;
case 4: cashEntered = getUserCash(drink);
getChange(drink, userChoice, cashEntered);
getTotalIncome(drink, userChoice, totalIncome);
clear();
break;
case 5: cashEntered = getUserCash(drink);
getChange(drink, userChoice, cashEntered);
getTotalIncome(drink, userChoice, totalIncome);
clear();
break;
case 6: cout << "Total income: " << totalIncome; ;
break;
}
} while (userChoice != 6 ); // do loop runs til score limit is reached
system("PAUSE");
return EXIT_SUCCESS;
}
// Function to retreive data from drinks.txt
void getFileData(DrinkInfo drink[], int NUM_ELEMENTS)
{
// File drinks.txt read data into structure variables arrays
fstream inFile;
inFile.open("drinks.txt", ios::in);
if (!inFile) // in case we cannot open the file, signal the error and exit
{
cout << "ERROR: Cannot open file\n";
system("PAUSE");
//return EXIT_SUCCESS;
}
// Loop to retrieve data from file, and store in
// arrays
for(int index = 0; index < NUM_ELEMENTS; index++)
{
inFile.getline(drink[index].name, SIZE, ',');
// Function t0 display menu, and return choice for switch
// statement.
int userSelectionMenu()
{
int choice;
// top of program game description
cout << right << setw(50) << "Vending Machine!\n\n";
cout << right << setw(47) << "1. Coca-Cola\n";
cout << right << setw(47) << "2. Root Beer\n";
cout << right << setw(44) << "3. Sprite\n";
cout << right << setw(50) << "4. Spring Water\n";
cout << right << setw(49) << "5. Apple Juice\n";
cout << right << setw(43) << "6. Exit\n\n";
cout << right << setw(55) << "Please choose a bevarage: ";
cin >> choice;
cout << "\n";
// Validate input is within range
while (choice < 1 || choice > 6)
{
cout << "Invalid choice. Enter a value between 1-5 or 6 to exit ";
cin >> choice;
}
return choice;
}
// Function for user to enter cash and verifies
// if amount is valid
double getUserCash(DrinkInfo drink[])
{
double cash;
cout << "Please insert money: ";
cin >> cash;
while(cash < 0 || cash > 1.00)
{
cout << "Invalid amount, please enter a amount not exceeding $1.00: ";
cin >> cash;
}
return cash;
}
// Function to get change for transaction if there is any
double getChange(DrinkInfo drink[], int choice, double cashEntered)
{
double change;
void getTotalIncome(DrinkInfo drink[], int choice, float &total)
{
if(choice == 1)
{
total += drink[0].price;
}
else if(choice == 2)
{
total += drink[1].price;
}
else if(choice == 3)
{
total += drink[2].price;
}
else if(choice == 4)
{
total += drink[3].price;
}
else if(choice == 5)
{
total += drink[4].price;
}
}
void clear()
{
system("PAUSE"); // Await users input to proceed to next round
system("cls");
}
// Here is where im having the problem. It appears to subtract
// the drink from the array, but when i come back to it it seems
// to reset.
void quantityLogic(DrinkInfo drink[], int choice)
{