Passing a Variable from Main into a Function

Hi! I'm currently working on a group project for my computer science course. For some reason, we can't figure out how to pass a variable from the main (num_days)
into the function getTime. We have to record the time of arrival for every day through a function. All advice is appreciated! :)

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
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <iostream>
using namespace std;

// Function Prototypes

int getDays(int);
int getTime (int); 

int main()
{

	int 			hour_ariv, min_ariv, hour_dep, min_dep; // time of arrival and departure

	double     		roud_air, // cost of airfare (roundtrip) 
    				car_rent, // cost of car rental 
    				choice_car, // allows to choose car_rent or priv_car
    				park_fee, // if park_fee > 12 , employee pays (PER DAY)
    				taxi_fee, // if taxi_fee > 40 , employee pays (PER DAY)
					regis_fee, // Conference or seminar fees
					hotel_pay, // if hotel_pay > 90 (PER NIGHT), employee pays
					breakfast_fee, lunch_fee, dinner_fee, // breafast > 18, lunch > 12, dinner > 20, employee pays 
					tot_breakfast, tot_lunch, tot_dinner, // collective totals for meals 
					tot_expenses, // total expenses allowed for entire trip
					tot_spent, // total expenses spent during the entire trip
					tot_employee_pay, // total expenses employee pays
					totexp_allow_car, // total expenses allowed for private vehicle 
					totexp_allow_park, // total expenses allowed for parking
					totexp_allow_taxi, // total expenses allowed for taxis 
					totexp_allow_hotel, // total expenses allowed for hotel 
					totexp_allow_meals, // total expenses allowed for meals (combined)
					totexp_allow_breakfast, // total allowed for breakfast 
					totexp_allow_lunch, // total allowed for lunch 
					totexp_allow_dinner; // total allowed for dinner
		
	const double	vech_per_mile = 0.58; 
	
	cout << "*******************************************************" << endl; 
	cout << "*             BUSINESS TRAVELLING EXPENSES            *" << endl;
	cout << "*                                                     *" << endl;
	cout << "*******************************************************" << endl; 
	
	// Calling of Functions
	
	int num_days = getDays (num_days);
	
	for (int i = 1; i <= num_days; i++)
	{
		int getTime (hour_ariv);
	}

	
	// Output 
	



 
 system ("pause");
 return 0;   

}

int getDays (int num_days)
{
	cout << "How many days were spent on the trip?" << endl; 
	cout << "Numbers of Days: "; 
	cin >> num_days; 
	cout << endl << endl; 
	
	return num_days;
}

int getTime (int hour_ariv)
{
	for (int i = 1; i <= num_days; i++)
	{
		cout << "On Day" << i << "what time did you arrive?" << endl; 
		cout << "Time of Arrival: "; 
		cin >> hour_ariv;
	
		return hour_ariv;	
	}
	

}
Hello jesseb96,

Short question. In the function getTime() where is "num_days" defined?

Andy
I wanted the variable to be passed into the function from main. Would it still have to be initialized in some way in the function?
Line 49: What value do you think you're passing to getDays()? Hint: It's garbage because it hasn't been initialized yet. Poor style to pass uninitialized variables to functions.

Line 68: num_days is an output value and should be a local value inside getDays(). There is no reason to pass it in by value.

You have a couple of problems with getTime().

Line 80: You're looping for num_days. num_days is NOT visible in getTime(). Since you need it, you should pass it in as an argument.

Line 86: This is going to cause you to return during the first iteration.





Topic archived. No new replies allowed.