Functions

Having a problem I have to display a table with values of different activities using the very basic methods of functions with out references. I got the prompts to ask for the values from the user and have my calculations in that I believe will use those values from the prompts but I can not get either one of my void functions to display. The ones with my actual values I haven't put in just yet because the first function doesn't work so that is why they are not in. Also when I go through the prompts it keeps asking the same questions. Any help will be very appreciated thank you.
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <iomanip>
#include <string> 

 using namespace std;



 int inputNumber(string);

 int calculateCalories_Hours(int);
 
 int calculateCalories_Mins(int);

 double calculateCalories(double, int, int);
 
 void displayHeadings();

 void displayLine(int, int, double);

int main()
{
	string			name;

	int				user_Weight,
					weight,
					badminton_Mins_User,
					running_Mins_User,
					walking_Mins_User,
					weight_Training_Mins_User,
					badminton_Hours_Amount,
					running_Hours_Amount,
					walking_Hours_Amount,
					weight_Training_Hours_Amount,
					badminton_Mins_Amount,
					running_Mins_Amount,
					walking_Mins_Amount,
					weight_Training_Mins_Amount;

	double			badminton_Cals_Amount,
					running_Cals_Amount,
					walking_Cals_Amount,
					weight_Training_Cals_Amount;

	const double	BADMINTON_BURN_RATE_PLB = 0.044,
					RUNNING_BURN_RATE_PLB = 0.087,
					WALKING_BURN_RATE_PLB = 0.036,
					WEIGHT_TRAINING_BURN_RATE_PLB = 0.042;
	
	cout << "Welcome to The Workout calculator." << endl;

	cout << "User, please enter your name: " << endl;
	getline(cin, name);

	weight = inputNumber( "Please enter your weight:");
	
	badminton_Mins_User = inputNumber("Please enter the minutes spent playing badminton: ");

	running_Mins_User = inputNumber("Please enter enter the minutes spent Running: ");

	walking_Mins_User = inputNumber("Please enter enter the minutes spent Walking: ");

	weight_Training_Mins_User = inputNumber("Please enter enter the minutes spent Weight-training: ");
	
	// badminton function calls
	badminton_Hours_Amount = calculateCalories_Hours(badminton_Mins_User);

	badminton_Mins_Amount = calculateCalories_Mins(badminton_Mins_User);
	
	badminton_Cals_Amount = calculateCalories(BADMINTON_BURN_RATE_PLB, weight, badminton_Mins_User);

	//running function calls
	running_Hours_Amount = calculateCalories_Hours(running_Mins_User);

	running_Mins_Amount = calculateCalories_Mins(running_Mins_User);

	running_Cals_Amount = calculateCalories(RUNNING_BURN_RATE_PLB, weight, running_Mins_User);

	//walking function calls 
	walking_Hours_Amount = calculateCalories_Hours(walking_Mins_User);

	walking_Mins_Amount = calculateCalories_Mins(walking_Mins_User);

	walking_Cals_Amount = calculateCalories(WALKING_BURN_RATE_PLB, weight, walking_Mins_User);

	//weight training function calls.
	weight_Training_Hours_Amount = calculateCalories_Hours(weight_Training_Mins_User);

	weight_Training_Mins_Amount = calculateCalories_Mins(weight_Training_Mins_User);
	
	weight_Training_Cals_Amount = calculateCalories(WEIGHT_TRAINING_BURN_RATE_PLB, weight, weight_Training_Mins_User);
			
	void displayHeadings();

	cout << "Here are the results for " << name << endl;

	void displayLine();

	system("PAUSE");
	return 0;
}

int inputNumber(string )// Takes the input from the user.
{
	string question_Weight,
			question_badminton_Mins,
			question_running_Mins,
			question_walking_Mins,
			questions_weight_Training_Mins;
	int		user_Weight=0,
			badminton_Mins=0,
			running_Mins=0,
			walking_Mins=0,
			weight_Training_Mins=0;
	
	question_Weight = "Please enter your weight: ";
	question_badminton_Mins ="Please enter the minutes playing Badminton: ";
	question_running_Mins = "Please enter the minutes spent running: ";
	question_walking_Mins = "Please enter the minutes spent walking: ";
	questions_weight_Training_Mins = "Please enter the minutes spent weight training: ";
	
	cout << question_Weight;
	cin >> user_Weight;
	
	cout << question_badminton_Mins ;
	cin >> badminton_Mins;

	cout << question_running_Mins;
	cin >> running_Mins;

	cout << question_walking_Mins;
	cin >> walking_Mins;
	
	cout <<questions_weight_Training_Mins;
	cin >> weight_Training_Mins;
	return (user_Weight, badminton_Mins, running_Mins, walking_Mins, weight_Training_Mins);

}


int calculateCalories_Hours(int a) //calculates the hours.
{
	
	return(a/60);
}

int calculateCalories_Mins(int a) //calculates the minutes.
{
	
	return (a&60);
}

double calculateCalories(double cons_Burn_Rate, int weight_User, int mins_User)
{
	return (cons_Burn_Rate*weight_User*mins_User);
}



void displayHeadings()
{
	cout << "\nActivity" << setw(16) << "Time" << "  " << "Calories\n";
	//The setw puts in how many spaces between activity and Time. 

	cout << "----------------------------------\n" << endl;
}
void displayLine(int mins_Excercise_Mins, int hours_Exercise_Totals, double Cals_Burned, int total_Hours, int total_Minutes, double total_Cals_Burned)
{
	cout << setprecision(3) << fixed;

	cout << "Badminton" << setw(12) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "Running" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;
	
	cout << "Walking" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "Weights" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "----------------------------------\n";

	cout << "Totals" << setw(15) << total_Hours << ":" << setfill('0') << setw(2) << total_Minutes
		<< setfill(' ') << "  " << setw(7) << total_Cals_Burned << endl;

}
I assume by "the first function" you mean inputNumber()?

A function can only return a single value (where a "value" can be an object, not just a numerical value). You've defined the function to return an int, which means you've defined it to return a single integer.

Now look at the return statement:

return (user_Weight, badminton_Mins, running_Mins, walking_Mins, weight_Training_Mins);

What single number is that going to return?

(Hint: if you're going to use the comma operator, it might be a good idea to look up what it actually does.)

If you want your function to pass multiple values, your options are:

1) Have it pass multiple arguments by reference.
2) Pack those values into a single object, and return that object.

EDIT: Also, look at how you've defined the function:

int inputNumber(string )

What's the point of passing in a string? You're not using that argument - you've not even given it a name! - so why did you put it there?
Last edited on
Ok from running the program it does grab the values from the user. return (user_Weight, badminton_Mins, running_Mins, walking_Mins, weight_Training_Mins);

Then should I be writing more definitions to fit the each value because what I got it has been getting those values. And What I am really trying to do is put those values through the display line without referencing.

edit: I see what your saying and fixed it to get the one value.

The problem is I have very little info to do this and me an a bunch of other students are having the same issues with very little resources to touch on.
Thats why I came on here to figure out how to do this without using reference.
Last edited on
This is what I currently have i have fixed everything except getting the table to actually display.
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;



 int inputNumber(string);

 int calculateCalories_Hours(int);
 
 int calculateCalories_Mins(int);

 double calculateCalories(double, int, int);
 
 void displayHeadings();

 void displayLine(int, int, double);

int main()
{
	string			name;

	int				user_Weight,
					weight,
					badminton_Mins_User,
					running_Mins_User,
					walking_Mins_User,
					weight_Training_Mins_User,
					badminton_Hours_Amount,
					running_Hours_Amount,
					walking_Hours_Amount,
					weight_Training_Hours_Amount,
					badminton_Mins_Amount,
					running_Mins_Amount,
					walking_Mins_Amount,
					weight_Training_Mins_Amount,
					total_Time,
					total_Hours,
					total_Mins;

	double			badminton_Cals_Amount,
					running_Cals_Amount,
					walking_Cals_Amount,
					weight_Training_Cals_Amount,
					total_Cals_Burned;

	const double	BADMINTON_BURN_RATE_PLB = 0.044,
					RUNNING_BURN_RATE_PLB = 0.087,
					WALKING_BURN_RATE_PLB = 0.036,
					WEIGHT_TRAINING_BURN_RATE_PLB = 0.042;
	
	cout << "Welcome to the Workout calculator." << endl;

	cout << "User, please enter your name: " << endl;
	getline(cin, name);

	weight = inputNumber( "Please enter your weight:");
	
	badminton_Mins_User = inputNumber("Please enter the minutes spent playing badminton: ");

	running_Mins_User = inputNumber("Please enter enter the minutes spent Running: ");

	walking_Mins_User = inputNumber("Please enter enter the minutes spent Walking: ");

	weight_Training_Mins_User = inputNumber("Please enter enter the minutes spent Weight-training: ");
	
	cout << "Here are the results for " << name << endl;

	 displayHeadings();

	// badminton function calls
	badminton_Hours_Amount = calculateCalories_Hours(badminton_Mins_User);

	badminton_Mins_Amount = calculateCalories_Mins(badminton_Mins_User);
	
	badminton_Cals_Amount = calculateCalories(BADMINTON_BURN_RATE_PLB, weight, badminton_Mins_User);

	//running function calls
	running_Hours_Amount = calculateCalories_Hours(running_Mins_User);

	running_Mins_Amount = calculateCalories_Mins(running_Mins_User);

	running_Cals_Amount = calculateCalories(RUNNING_BURN_RATE_PLB, weight, running_Mins_User);

	//walking function calls 
	walking_Hours_Amount = calculateCalories_Hours(walking_Mins_User);

	walking_Mins_Amount = calculateCalories_Mins(walking_Mins_User);

	walking_Cals_Amount = calculateCalories(WALKING_BURN_RATE_PLB, weight, walking_Mins_User);

	//weight training function calls.
	weight_Training_Hours_Amount = calculateCalories_Hours(weight_Training_Mins_User);

	weight_Training_Mins_Amount = calculateCalories_Mins(weight_Training_Mins_User);
	
	weight_Training_Cals_Amount = calculateCalories(WEIGHT_TRAINING_BURN_RATE_PLB, weight, weight_Training_Mins_User);
			
	total_Time = badminton_Mins_User + running_Mins_User + walking_Mins_User + weight_Training_Mins_User;

	total_Cals_Burned = badminton_Cals_Amount + running_Cals_Amount + walking_Cals_Amount + weight_Training_Cals_Amount;

	total_Hours = total_Time / 60;

	total_Mins = total_Time % 60;

	void displayLine(int badminton_Hours_Amount, int badminton_Mins_Amount, double badminton_Cals_Amount, int running_Hours_Amount, int running_Mins_Amount,
		double running_Cals_Amount, int walking_Hours_Amount, int walking_Mins_Amount, double walking_Cals_Amount, int weight_Training_Hours_Amount, int weight_Training_Mins_Amount,
		double weight_Training_Cals_Amount, double total_Cals_Burned, int total_Hours, int total_Mins);

	system("PAUSE");
	return 0;
}

int inputNumber(string prompt )// Takes the input from the user.
{
	
	int inputtedValue = 0; // declare a variable to hold a value
	
	cout << prompt; // output the prompt
	cin >> inputtedValue; // collect a number from the user
	
	return inputtedValue; // return the value inputted
	

}


int calculateCalories_Hours(int a) //calculates the hours.
{
	
	return(a/60);
}

int calculateCalories_Mins(int a) //calculates the minutes.
{
	
	return (a&60);
}

double calculateCalories(double cons_Burn_Rate, int weight_User, int mins_User)
{
	return (cons_Burn_Rate*weight_User*mins_User);
}

void displayHeadings()
{
	cout << "\nActivity" << setw(16) << "Time" << "  " << "Calories\n";
	//The setw puts in how many spaces between activity and Time. 

	cout << "----------------------------------\n" << endl;
}


void displayLine()
{
	int		mins_Excercise_Mins = 0, 
			hours_Exercise_Totals =0,
			total_Hours = 0,
			total_Minutes = 0 ;
	
	double	Cals_Burned =0 ,
			total_Cals_Burned = 0;

	cout << setprecision(3) << fixed;

	cout << "Badminton" << setw(12) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "Running" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;
	
	cout << "Walking" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "Weights" << setw(14) << hours_Exercise_Totals << ":" << setfill('0') << setw(2) << mins_Excercise_Mins
		<< setfill(' ') << "  " << setw(7) << Cals_Burned << endl;

	cout << "----------------------------------\n";

	cout << "Totals" << setw(15) << total_Hours << ":" << setfill('0') << setw(2) << total_Minutes
		<< setfill(' ') << "  " << setw(7) << total_Cals_Burned << endl;

}


If anyone can tell me how to get the last part I'd be much appreciative.
You're getting confused about the syntax for the following things:

- declaring a function
- defining a function
- calling a function

Look at line 108. I suspect that what you wanted to do here was to call the displayLine function, but the syntax you've used is the syntax for declaring a function (i.e. the same as you're doing in lines 8 - 18).

Also, you're not consistent about what arguments you want to pass into your function. At line 18, you declare that the function will take 3 arguments. At line 156, you define it to take no arguments. And in the attempt to call it at line 108, you're trying to pass a whopping 15 (I think arguments) to it.

Using functions is an absolutely fundamental part of programming in C and C++. My advice is, if you're in any way unclear about them, I strongly recommend you go back to your textbook and reread that section until you're totally clear. You're not going to get very far without it.
Topic archived. No new replies allowed.