Cannot initialize reference in a function.

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

 #include <cstdlib>
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;





struct Drink
{
    string DrinkName;
    double DrinkCost;
    int NumDrink;
};
int getChoice(Drink[]); //prototype function
void transaction(Drink[], int, double &);
 
int main()
{
 
const int NUM_SODAS = 5;
int drinkCh;
int index = 0;
double sodaChange;
double sodaMoney;
 
Drink drinkTable[NUM_SODAS];

    drinkTable[0].DrinkName = "Cola";
	drinkTable[0].DrinkCost = 0.75;
	drinkTable[0].NumDrink = 20;
	drinkTable[1].DrinkName = "Root Beer";
	drinkTable[1].DrinkCost = 0.75;
	drinkTable[1].NumDrink = 20;
	drinkTable[2].DrinkName = "Lemon-Lime";
	drinkTable[2].DrinkCost = 0.75;
	drinkTable[2].NumDrink = 20;
	drinkTable[3].DrinkName = "Grape Soda";
	drinkTable[3].DrinkCost = 0.80;
	drinkTable[3].NumDrink = 20;
	drinkTable[4].DrinkName = "Cream Soda";
	drinkTable[4].DrinkCost = 0.80;
	drinkTable[4].NumDrink = 20;
	
	
 do
 {	
 	cout<<"This program will show you a selection of sodas and their prices./n"<<endl<<endl;

	drinkCh = getChoice(drinkTable);
	if(drinkCh != 6)
	{
		cout<<"\nEnter money here:";
		cin>>sodaMoney;
		while(sodaMoney < 0.00 || sodaMoney > 1.00)
		{
			cout<<"\nPlease enter a valid amount of money: $";
			cin>>sodaMoney;
		}
	}
	////super stuck right here
	transaction(drinkTable, drinkCh, &moneyCh);
	cout<<endl<<endl;
 }while (drinkCh != 6);
return 0;
}

int getChoice(Drink drinkTable[5])
{
	int sodaCh; //soda selection
	int count = 0;
	int drinkPick;
	
	do
	{
		//works
		for(count = 0; count < 5; count++)
                {		
                    cout<<count + 1<<": "<<drinkTable[count].DrinkName<<setw(8)<<"\tCost: "<<drinkTable[count].DrinkCost<<"\tInventory: "<<drinkTable[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 select to exit the program by entering the number 6."<<endl;
                cout<<"Select Soda: ";
                cin>>sodaCh;
               
                while(sodaCh < 1 || sodaCh >6)
                {
                	cout<<"Please enter a valid selection: numbers 1-6."<<endl;
                	cin>>sodaCh;
				}
				if(sodaCh != 6)
				{
					drinkPick = sodaCh - 1;
					
					if(drinkTable[drinkPick].NumDrink <= 0)
					{
						cout<<drinkTable[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;
						}
					}
					--drinkTable[drinkPick].NumDrink;
				}
				
		cout<<endl;
		return sodaCh;
	}while(sodaCh != 6);
}
void transaction(struct drink[5], int drinkSel, &double money)
{
	int sodaPick;
	double change;
	sodaPick = drinkSel - 1;
	if(money > drink[sodaPick].DrinkCost)
	{
		change = money - drinkTable[sodaPick].DrinkCost;
		cout<<change;
	}
Last edited on
Hello hunt555,

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.

Hope that helps,

Andy
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.

transaction function is missing closing brace.
Last edited on
UPDATE- I was able to get the program working the problem now is returning the value calculated in the transaction function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;





struct Drink
{
    string DrinkName;
    double DrinkCost;
    int NumDrink;
};
int getChoice(Drink[]); //prototype function
void transaction(Drink[], int, double &);
 
int main()
{
 
const int NUM_SODAS = 5;
int drinkCh;
double sodaChange;
double sodaMoney;
 
Drink drinkTable[NUM_SODAS];

    drinkTable[0].DrinkName = "Cola";
	drinkTable[0].DrinkCost = 0.75;
	drinkTable[0].NumDrink = 20;
	drinkTable[1].DrinkName = "Root Beer";
	drinkTable[1].DrinkCost = 0.75;
	drinkTable[1].NumDrink = 20;
	drinkTable[2].DrinkName = "Lemon-Lime";
	drinkTable[2].DrinkCost = 0.75;
	drinkTable[2].NumDrink = 20;
	drinkTable[3].DrinkName = "Grape Soda";
	drinkTable[3].DrinkCost = 0.80;
	drinkTable[3].NumDrink = 20;
	drinkTable[4].DrinkName = "Cream Soda";
	drinkTable[4].DrinkCost = 0.80;
	drinkTable[4].NumDrink = 20;
	
	
 do
 {	
 	cout<<"This program will show you a selection of sodas and their prices./n"<<endl<<endl;

	drinkCh = getChoice(drinkTable);
	if(drinkCh != 6)
	{
		cout<<"\nEnter money here:";
		cin>>sodaMoney;
		while(sodaMoney < 0.00 || sodaMoney > 1.00)
		{
			cout<<"\nPlease enter a valid amount of money: $";
			cin>>sodaMoney;
		}
	}
	////super stuck right here
	void transaction(Drink drink[], int drinkSel, double &sodaMoney);
	cout<<endl<<endl;
 }while (drinkCh != 6);
return 0;
}

int getChoice(Drink drinkTable[5])
{
	int sodaCh; //soda selection
	int count = 0;
	int drinkPick;
	
	do
	{
		//works
		for(count = 0; count < 5; count++)
                {		
                    cout<<count + 1<<": "<<drinkTable[count].DrinkName<<setw(8)<<"\tCost: "<<drinkTable[count].DrinkCost<<"\tInventory: "<<drinkTable[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 select to exit the program by entering the number 6."<<endl;
                cout<<"Select Soda: ";
                cin>>sodaCh;
               
                while(sodaCh < 1 || sodaCh >6)
                {
                	cout<<"Please enter a valid selection: numbers 1-6."<<endl;
                	cin>>sodaCh;
				}
				if(sodaCh != 6)
				{
					drinkPick = sodaCh - 1;
					
					if(drinkTable[drinkPick].NumDrink <= 0)
					{
						cout<<drinkTable[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;
						}
					}
					--drinkTable[drinkPick].NumDrink;
				}
				
		cout<<endl;
		return sodaCh;
	}while(sodaCh != 6);
}
void transaction(Drink drink[], int drinkSel, double &sodaMoney)
{
	int sodaPick;
	double change;
	sodaPick = drinkSel - 1;
	if(sodaMoney > drink[sodaPick].DrinkCost)
	{
		change = sodaMoney - drink[sodaPick].DrinkCost;
		cout<<"\nYour change will be: "<<change<<endl<<endl;
	}
	else
	{
		cout<<"\nThat is not enough money to buy a soda, please enter a larger amount of money for this transaction: ";
		cin>>sodaMoney;
	}
}
Last edited on
I have figured it out. thank you all for helping me out. I will post what I did for future reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;

struct Drink//structure 
{
    string DrinkName;
    double DrinkCost;
    int NumDrink;
};
//prototype functions
int getChoice(Drink[]); 
void transaction(Drink[], int, double &);
 
int main()
{
 
const int 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;
			}
			else if(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;
}
Topic archived. No new replies allowed.