How do I set up function for input .

Hi guys , so here I am stucked with my coding , the assessment is to convert from normal program to function program. I almost done but there's one problem. How do I set up function for input ?

Here is the program working perfectly
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
#include <iostream>
using namespace std;
//*Declaration*//
float input ();
float process (float,float,float);
void display (float);

int main(){

	float a,b,c,result,myinput,steps,minutes,seconds;

	cout << "Enter the steps , minutes, seconds = " << endl;
	cin >> steps >> minutes >> seconds ;

	//*Function Call*//
	result = process(steps,minutes,seconds);
	display(result);
	return 0;
}

	
float process (float steps, float minutes, float seconds){

	float result;
	result = (minutes+seconds) / 60 ;
	result = result * 2.5 ;
	result = result / 5280 ;
	result = result * steps;
	return result;
}
void display (float result){

	cout << "The total distance is " << result << " miles" << endl;
	return ;
}



As you can see that I have the function for display and process. One more thing is to create the function for input. The coding perfectly working on this one.

So here is the coding after I tried to create the function for input sadly the output only shows 0. Can I know where did I got wrong ?

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
#include <iostream>
using namespace std;
float input ();
float process (float,float,float);
void display (float);

int main(){
	float result,myinput,steps,minutes,seconds;

	//*Function Call*//

	myinput = input(steps,minutes,seconds);
	result = process(steps,minutes,seconds);
	display(result);
	return 0;
}
float input (){

	float steps,minutes,seconds;
	cout << "Enter the steps , minutes, seconds = " << endl;
	cin >> steps >> minutes >> seconds ;
	return steps,minutes,steps;
}
	
float process (float steps, float minutes, float seconds){

	float result;
	result = (minutes+seconds) / 60 ;
	result = result * 2.5 ;
	result = result / 5280 ;
	result = result * steps;
	return result;
}
void display (float result){

	cout << "The total distance is " << result << " miles" << endl;
	return ;
}


Last edited on
Line 22: you can't return 3 values like that. See the description of the comma operator:
http://www.cplusplus.com/doc/tutorial/operators/

Line 17: You need to pass steps, minutes, seconds by reference.

17
18
19
20
void input(float & steps, float & minutes, float & seconds) 
{   cout << "Enter the steps , minutes, seconds = " << endl;
    cin >> steps >> minutes >> seconds;    
}

Don't forget to change your function prototype to match.
Last edited on
You can only "return" one item from a function with the return statement. You may want to consider passing the variables by reference into the function.

By the way your function implementation, function prototype, and the function call must all agree as to the number and types of parameters.

Last edited on
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
#include <iostream>
using namespace std;
//*Declaration*//
float input ();
float process ();
void display (float);

int main(){

	//*Function Call*//
    float result = process();
	display(result);
	return 0;
}

	
float process (){
    
    float result,steps,minutes,seconds;

	cout << "Enter the steps , minutes, seconds = " << endl;
	cin >> steps >> minutes >> seconds ;

	result = (minutes+seconds) / 60 ;
	result = result * 2.5 ;
	result = result / 5280 ;
	result = result * steps;
	return result;
}
void display (float result){

	cout << "The total distance is " << result << " miles" << endl;
	return ;
}


You can move most of your code into the function.
Topic archived. No new replies allowed.