function not returning variables

My professor in an introductory C++ class asked us to take a program, and debug it, and have solved most of the bugs present in said code, however, I cannot figure out why the convert function doesnt seem to be transferring the converted values to the rest of the program, and thus gives a printout of 0 feet, 0 inches at the end.

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
#include <iostream>
#include <iomanip>

using namespace std;

const double CM_PER_INCH = 2.54;				// othere are 2.54 cm in one inch
const int INCH_PER_FOOT = 12;					// there are 12 inches in one foot

int getHeight();
int myHeight;
int convert(int, int, int);
void displayResult(int, int, int);

int main()
{
	int myHeight = 0;
	int feet = 0;
	int inches = 0;

	getHeight();

	convert(myHeight, feet, inches);

	displayResult(myHeight, feet, inches);


	return 0;
}

int getHeight()
{
	cout << "Enter your height in centimeter: ";
	cin >> myHeight;

	return myHeight;
}

int convert(int myHeight, int feet, int inches)
{
	int totalInch = static_cast<int>(myHeight / CM_PER_INCH + 0.5);

	feet = totalInch / INCH_PER_FOOT;
	inches = totalInch % INCH_PER_FOOT;
return (feet, inches);
}

void displayResult(int cm, int feet, int inches)
{
	cout << fixed << showpoint << setprecision(1);

	cout << endl << myHeight << " centimeter is equivalent to:\n"
		 << feet << " feet and "
	     << inches << " inch\n";
}
Do you mean that version of convert() or the fixed version?
the convert() should make the variables "feet", and "inches" what is displayed at the end of the program, however, that doesnt happen, all that happens is it displays the values they were given at the beginning of the program, there inlies the problem.
1. don't use global variables. Remove int myHeight; at line 10.

2. do something with the value returned from getHeight(). Line 20:
 
    myHeight = getHeight();

3. inside function getHeight(), declare a local variable int myHeight; before the cin.

4. change the return type of convert() from int to void. Remove the return statement at line 44.

5. change the function prototype and header and (lines 11 and 38) to pass feet and inches by reference.
 
void convert(int myHeight, int & feet, int & inches)


6. change function displayResult() so that it actually uses the parameter named cm.

7. read the tutorial on functions. http://www.cplusplus.com/doc/tutorial/functions/

(it may be better to do item 7 first).

Last edited on
Registered users can post here. Sign in or register to post.