Does not work!!

Write a program that reads in a length in feet and inches and outputs the equivalent length in meters and centimeters. Use at least three functions: one for input, one or more for calculating, and one for output. Include a loop that lets the user repeat this computation for new input values until the user says he wants to end the program. There are 0.3048 meters in a foot, 100 centimeters in a meter, and 12 inches in a foot.

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
#include <iostream>
using namespace std;
void get_input(double& feet, double& inches, double& meters, double& centimeters);
	
double convert(double& feet, double& inches, double& meters , double& centimeters);

void give_output(double feet, double inches, double meters, double centimeters);

	int main()
{
	double feet, inches, meters, centimeters;
	char check;

	do {
			get_input(feet, inches, meters, centimeters);
			convert(feet, inches, meters, centimeters);
			give_output(feet, inches, meters, centimeters);
			cout<<"Repeat calculation? (Y/N)"<<endl;
			cin>>check;

		}while(check == 'Y' || check == 'y');

	system("pause");
	return 0;
}

void get_input(double& feet, double& inches, double& meters, double& centimeters)
{ 
	
	cout<<"Enter feet to convert it to meters: ";
	cin>>feet;
	cout<<endl<<"Enter inches to convert it to centimeters: ";
	cin>>inches;
}

double convert(double& feet, double& inches, double& meters , double& centimeters)
{
	
	meters = feet * 0.3048;
	return meters;
	centimeters = inches * 2.54;
	return centimeters;
}

void give_output(double feet, double inches, double meters, double centimeters)
{
	cout<<feet<<" feet " <<inches<<" inches is equivalent to "<<meters<<" meters "<<centimeters<<" centimeters"<<endl;
}



Can someone please help me, i can not see where is my mistake!

it coverts from feet to meters correctly, but when it comes to inches the output is incorrect ---->
-9.25596e+061
<---- this is the output for any number.
Last edited on
remove the semicolons on line 27 and 45.

The parameters should be passed by reference in get_input
ahaaa, thanks alot man.
Oh my god, now it is telling me that i have run time error!! because i did not initialize the variable 'centimeters' and 'meters'. :S where should i initialize them?
Run time errors? I get compile time warnings saying that.

The problem is that you have declared meters and centimeters locally inside the functions so they will not exist when the function returns. You probably want to pass them as argument or use return value or whatever.
Thanks m8, but still something is missing!! it coverts from feet to meters correctly, but when it comes to inches the output is incorrect ----> -9.25596e+061 <---- this is the output for any number.
1
2
3
4
5
6
7
8
double convert(double& feet, double& inches, double& meters , double& centimeters)
{
	
	meters = feet * 0.3048;
	return meters;
	centimeters = inches * 2.54;
	return centimeters;
}


This is a nonsense. These lines:
1
2
	centimeters = inches * 2.54;
	return centimeters;

will never ever be run, because the function returns before then. Why do you even bother returning a value? You don't use it and you're passing by reference so you don't even need it.
Thought if i am using double type there should be a return. if i removed the return the program would not run.
You don't need any return statements. The variables are all passed by reference. If I were you, I would just make the convert() function a void return type as well, then you don't have to worry about it.
Thanks everybody, specially azuk040891 :D i changed it to void and it's working perfectly 8).
Topic archived. No new replies allowed.