how to pass values of variable received from function A to function B

I am trying to pass value received by variable person and day from function getPersons(person1)and getDays(days1)to function advWeekend(person,day) i cannot see the values being transferred correctly, it just shows value of person as 0 and days as 1 inside function advWeekend(person,day). Any suggestion what i am missing not to be able to pass values of person and day to function advWeekend(person,day). Thankyou.

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
136
137
138
139
140
141
142
143
144
145
146
147
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

void getMenu();
int getChoice(int);
int getPersons(int);
int getDays(int);
void advWeekend( int , int );
//void scubaBahama(string, int, int);
//void skyDive(string, int, int);
//void baronCliff(string, int, int);
//int discount(double);


int main()
{
	int choice,
		person,
		day,
		person1,
		days1;
		
	getMenu();
	getChoice(choice);
	
	
	person = getPersons(person1);
	day = getDays(days1);

//	cout<<person<<endl; // i can see latest value received from getDays(days1)
//	cout<<day; // i can see latest value received from getDays(days1)

	advWeekend(person,day);

//my expectation is to pass value of person and day to advWeekend(person,day)
// and use it to do calculations for rate inside advWeekend(person,day)

	system("pause");
	return 0;
	
}

void getMenu()
{
	cout<<"The High Adventure Travel Agency \n";
	cout<<"1. Devil's Courthouse Adventure Weekend \n";
	cout<<"2. Scuba Bahama \n";
	cout<<"3. Sky-Dive Colorado \n";
	cout<<"4. Barron Cliff Spelunk \n";
	cout<<"5. Exit \n";
	
}

int getChoice(int selection)
{
	int choice;
	cout<<"Enter valid choice between 1-5 ";
	cin>>choice;
	while(choice<=0 || choice>5)
	{
		cout<<"Please enter valid choice between 1-5 \n";
		cout<<"Your choice must be between 1-5 \n";
		cin>>choice;
	}
	return(choice);
}

int getPersons(int noPersons)
{
	int persons;
	
	cout<<endl;
	cout<<"Enter number of persons to participate ";
	cin>>persons;
	while(persons<=0)
	{
		cout<<"You cannot enter number of persons as zero(0) \n";
		cout<<"Please enter number of persons to participate \n";
		cin>>persons;
	}
	return(persons);
}

int getDays(int noDays)
{
	int days;
	
	cout<<endl;
	cout<<"Enter number of days ";
	cin>>days;
	while(days<3 || days>7)
	{
		cout<<"You cannot select number of days more than a week \n";
		cout<<"Maximum available days is 7 days per person \n";
		cout<<"Minimum availalbe days is 3 days per persons \n";
		cout<<"Enter number of days again ";
		cin>>days;
	}
	return(days);
	
}

void advWeekend( int noPerson, int noDay)
{
//int persons, 
//days;

// i was assigning another int variable persons and days as  
// i was expecting noPerson and noDay will transfer parameter value of noPerson 
// and noDay to persons and days
// i should have used noPerson and noDay parameters directly to use its value
// instead of assigning different varaialbes this is why i was not getting
 // correct value passed to persons and days in previous program.

   float multiply; // i am multiplying value of parameters noPerson & noDay
                         // and display the result
		
	cout<<endl;
	cout<<"Welcome to Devil's Courthouse Adventure Weekend \n";
	cout<<"Rates: \n";
	cout<<"Base Charge:\t\t\t $350.00/perosn \n";
	cout<<"Climbing Instruction:\t\t $100.00/person \n";
	cout<<"Equipment Rental:\t\t $40/day/person \n";

	cout<<"No of Persons " <<noPerson<<endl; 
        cout<<"No of Days " <<noDay<<endl;

//  now i can see value of person
// passed to function advWeekend it displays value of parameter noPerson
// now i can see value of days passed
// to function advWeekend, it displays value of parameter noDay

multiply = noPerson * noDay;

cout<<"Multiplication of value passed through parameter noPerson is "<<noPerson<<endl
<<" and value passed through parameter noDay is "<<noDay<<" is "<<multiply<<endl;
	
}






Last edited on
closed account (48T7M4Gy)
In line 102 you declare persons and then you print it out a line 111. days is the same situation.(When you declare a variable you should initialize it. Try -907 as an initial value for each and see what happens.)

Your function has parameters noPerson and noDay and there is no connection within the function between the two corresponding variables. Therein lies the problem!


Last edited on
Hi,
Thank you for your valuable time to put some input at my issues. My plan was to utilize the value of parameters noPerson and noDay to calcuale rates for different functions but due to reassignments of int variables int persons and days to pass value of noPerson and noDay was causing problem but now i understand what is the problem and updated program on which i can use original parameter value to calculate rates.

Thanks for your support.
I have updated the program the way i was expecting how it should work and is working according to my will.

Thanks again for your support.
Last edited on
Topic archived. No new replies allowed.