I am stuck when trying to initialize my double variable in my function transaction(drink, int, double&), can someone please explain how I can initialized it?
What jumps out at me is line 19 and 116. Both are likely to wok better as: void transaction(Drink drink[], int drinkSel, &double money)
Having a value between the [] is not necessary, but OK. Also I would make both places match. The proto type is fine, but the function definition does not match.
Thanks Handy Andy, when compiling that correction it said that it expected an identifier before &,
' , ' or '...'
Im not sure what this means since i have already separated my drink int and double with commas, thank you for your help.
To clarify what im trying to do is to pass the sodaMoney variable in my main function to the transaction function where i will calculate the change due and return that value to the main function.
transaction(drinkTable, drinkCh, &moneyCh);
moneyCh isn't actually defined anywhere. Did you mean to use sodaMoney? Also you don't need the & there.
void transaction(struct drink[5], int drinkSel, &double money)
Should be double& money
change = money - drinkTable[sodaPick].DrinkCost;
drinkTable isn't known about in the transaction function, you pass it as "drink[5]" (you should keep your names the same in the entire program).
You are just setting change to a local variable in transaction, not returning it to main.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include<string>
usingnamespace std;
struct Drink//structure
{
string DrinkName;
double DrinkCost;
int NumDrink;
};
//prototype functions
int getChoice(Drink[]);
void transaction(Drink[], int, double &);
int main()
{
constint NUM_SODAS = 5;
int drinkCh;//drink choice number
int sodaPick;//soda that was picked by numerical value
double sodaChange;//change that is due after transaction
double sodaMoney;//amount of money entered by the user
Drink drink[NUM_SODAS];//structure values
drink[0].DrinkName = "Cola";
drink[0].DrinkCost = 0.75;
drink[0].NumDrink = 20;
drink[1].DrinkName = "Root Beer";
drink[1].DrinkCost = 0.75;
drink[1].NumDrink = 20;
drink[2].DrinkName = "Lemon-Lime";
drink[2].DrinkCost = 0.75;
drink[2].NumDrink = 20;
drink[3].DrinkName = "Grape Soda";
drink[3].DrinkCost = 0.80;
drink[3].NumDrink = 20;
drink[4].DrinkName = "Cream Soda";
drink[4].DrinkCost = 0.80;
drink[4].NumDrink = 20;
do //this loop shows the menu every time after a transaction
{
cout<<setprecision(2)<<fixed;
cout<<"This program will show you a selection of sodas and their prices./n"<<endl<<endl;
drinkCh = getChoice(drink);//get the number of the drink the user selected
if(drinkCh != 6)
{
sodaPick = drinkCh - 1; // to specify the memory address the drink the user selected on the structure
cout<<"\nEnter money here: $";
cin>>sodaMoney;
while(sodaMoney < 0.00 || sodaMoney > 1.00 || sodaMoney < drink[sodaPick].DrinkCost)//input validation
{
if(sodaMoney < 0.00 || sodaMoney > 1.00 )
{
cout<<"\nPlease enter a valid amount of money: $";
cin>>sodaMoney;
}
elseif(sodaMoney < drink[sodaPick].DrinkCost)
{
cout<<"\nYou did not enter enough amount of money, please look at the prices and enter the correct amount of my $";
cin>>sodaMoney;
}
}
if(sodaMoney >= drink[sodaPick].DrinkCost)//caculates the change owed to the user
{
sodaChange = sodaMoney - drink[sodaPick].DrinkCost;
cout<<setprecision(2)<<fixed;
cout<<"\nYour change will be: "<<sodaChange<<fixed<<endl<<endl;
}
}
////super stuck right here
void transaction(Drink drink[5], int drinkCh, double& sodaMoney);
}while (drinkCh != 6);
return 0;
}
int getChoice(Drink drink[5])
{
int sodaCh; //soda selection
int count = 0;// count for the FOR loop
int drinkPick;// help determine the memory address in the structure to select a drink
do
{
//works
for(count = 0; count < 5; count++)// shows menu
{
cout<<count + 1<<": "<<drink[count].DrinkName<<setw(8)<<"\tCost: "<<drink[count].DrinkCost<<"\tInventory: "<<drink[count].NumDrink<<endl<<endl;
}
cout<<"Please select the soda you would like to purchase by its coresponding number assigned on the menu, numbers 1-5. You can enter 6 to exit the program."<<endl;
cout<<"Select Soda: ";
cin>>sodaCh;
while(sodaCh < 1 || sodaCh >6)//input validation
{
cout<<"Please enter a valid selection: numbers 1-6."<<endl;
cin>>sodaCh;
}
if(sodaCh != 6)
{
drinkPick = sodaCh - 1;
if(drink[drinkPick].NumDrink <= 0)
{
cout<<drink[drinkPick].DrinkName<<": is out of stock please select another soda."<<endl<<endl;
cout<<"Select Soda: ";
cin>>sodaCh;
while(sodaCh < 1 || sodaCh >6)
{
cout<<"Please enter a valid selection: numbers 1-6."<<endl;
cin>>sodaCh;
}
}
--drink[drinkPick].NumDrink;//shows the user the inventory of the drinks available
}
cout<<endl;
return sodaCh;//returns the number the user inputed for the drink selection
}while(sodaCh != 6);
}
void transaction(Drink drink[5], int drinkCh, double& sodaMoney)
{
return;
}