Height in feet and inches converted to meters.

I've got a problem with this program. For some reason, it's not doing the calculations and it's not asking if the user would like run the program again. It simply asks for the feet and then the inches over and over.

Any assistance would be 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
#include <iostream>
#include <string>

using namespace std; 

float heightInput(int, float);
float heightCalculating(int, float, float, float);
float heightOutput(float, float);
int feet;
float inches;
float meters;
float centimeters;

int main() 
{
	
    char again ='Y';// Identify sentinel as the letter Y
  while (again == 'y' || again == 'Y') // While loop to ask user if they would like to re-run program.
  {
         cout << "Please input the number of feet "; // Asking user for the number of feet
         cin >> feet;
         cout << "Please input the number of inches: "; //Asking user for the number of inches
         cin >> inches;
   }
   {
   	cout << "\n\nTo run the program again enter y or Y)."; // Ask user if they would like to re-run the program
         cin >> again ;
   }
    return 0;
}//end main

float heightInput(int feet, float inches)
{
    cout << "Enter feet: ";
    cin >> feet;
    cout << "Enter inches: ";
    cin >> inches;
}
float heightCalculating(int feet, float inches)
{
    feet = meters * 0.03048;
    centimeters = inches * 2.54;
    
}
float heightOutput(float meters, float centimeters)
{
    cout << meters << " meters and " << centimeters <<
            " centimeters.\n";
}
I've got a problem with this program.
A genteelly understatement?
Delete lines 24 and 25, then user has the option to end the program. Alas, with no conversion ever.
> It simply asks for the feet and then the inches over and over.
1
2
3
4
5
6
7
8
	char again ='Y';// Identify sentinel as the letter Y
	while (again == 'y' || again == 'Y') // While loop to ask user if they would like to re-run program.
	{
		cout << "Please input the number of feet "; // Asking user for the number of feet
		cin >> feet;
		cout << "Please input the number of inches: "; //Asking user for the number of inches
		cin >> inches;
	}
that loop is in your main() function
notice that `again', the variable used in the condition, is not touched at all in the body of the loop
¿how do you expect the loop to end if the condition never changes?


> it's not doing the calculations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() 
{

	char again ='Y';// Identify sentinel as the letter Y
	while (again == 'y' || again == 'Y') // While loop to ask user if they would like to re-run program.
	{
		cout << "Please input the number of feet "; // Asking user for the number of feet
		cin >> feet;
		cout << "Please input the number of inches: "; //Asking user for the number of inches
		cin >> inches;
	}
	{
		cout << "\n\nTo run the program again enter y or Y)."; // Ask user if they would like to re-run the program
		cin >> again ;
	}
	return 0;
}
that's your main function
¿where are you doing any calculation or calling any function?


1
2
3
4
5
float heightCalculating(int feet, float inches)
{
	feet = meters * 0.03048;
	centimeters = inches * 2.54;
}
one of your functions.
you promise to return a float, but there is no return statement (an error that you repeat in the other functions)
`feet' is a parameter passed by value, any changes made to it will be local to this function
but your logic is egregious, you are trying to convert to meters, so it should be meters = alpha*feet + beta*inches

also, avoid those global variables.
1. Avoid global variables if at all possible.
2. Pass your variables into your functions by reference or by value.
3. Where are your return statements in your functions? Make the return types void.

4. And the biggie, your conversion from feet/inches to meters/centimeters is flawed.

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
#include <iostream>
#include <cctype>   // for toupper
#include <cmath>    // for trunc

void getInput(int&, float&);
void heightCalculating(int, float, float&, float&);
void heightOutput(float, float);

int main()
{
   int feet;
   float inches;
   float meters;
   float centimeters;

   char again = 'Y';
   
    // while loop to ask user if they would like to re-run program.
    while (::toupper(again) == 'Y')
   {
      getInput(feet, inches);

      heightCalculating(feet, inches, meters, centimeters);

      heightOutput(meters, centimeters);

      // ask user if they would like to re-run the program
      std::cout << "\n\nTo run the program again enter 'Y'): ";
      std::cin >> again;
   }
}

void getInput(int& feet, float& inches)
{
   std::cout << "Enter feet: ";
   std::cin >> feet;
   
   std::cout << "Enter inches: ";
   std::cin >> inches;
}

void heightCalculating(int feet, float inches, float& meters, float& centimeters)
{
   // you're forgetting to combine feet and inches to get meters/centimeters
   // you are calculating them separately with a flawed formula

   float temp = feet + (inches / 12.0f);

   temp *= 0.3048f;
   
   // chop off the fractional part
   meters      = trunc(temp);
   centimeters = (temp - meters) * 100.0f;
}

void heightOutput(float meters, float centimeters)
{
   std::cout << '\n' << meters << " meters and " << centimeters << " centimeters.\n";
}

Enter feet: 3
Enter inches: 3

0 meters and 99.06 centimeters.


To run the program again enter 'Y'): y
Enter feet: 3
Enter inches: 3.5

1 meters and 0.330007 centimeters.


To run the program again enter 'Y'): Y
Enter feet: 6
Enter inches: 1

1 meters and 85.42 centimeters.


To run the program again enter 'Y'): n
Thanks for the advice everyone. I think I'm grasping what everyone is saying. I have a few questions though.

Furry Guy, what does the “&” after int and float, etc mean in void getInput, etc?

What is the point of the "f" after "12.0 in float temp = feet + (inches / 12.0f);"?

Also, what is the point of trunc? I can't find that anywhere in the book that I have on C++.

Once again thank you for your help.
Excellent questions.

What is the point of the "f" after "12.0

12.0 is normally treated by the compiler as being a double value. 12.0f tells the compiler it is a float value. Your variables are declared as floats. Using the "f" stops the compiler from possibly complaining about possible loss of data when converting from double to float.

It is not needed, but I use it as a reminder that I am working with floats instead of doubles.

what is the point of trunc?
trunc (or std::trunc) is a C library function that rounds off any decimal part of a double or float.
http://www.cplusplus.com/reference/cmath/trunc/

If you have 12.75 before using trunc(), it is 12.0 after. It chops off the decimal part leaving the whole number.

I can't find that anywhere in the book that I have on C++.

There is more to C and C++ than can fit into any one book. Poke around in the reference section here at cplusplus or cppreference. Be prepared to be overwhelmed by what is available. (And ask questions, always ask questions)

http://www.cplusplus.com/reference/
https://en.cppreference.com/w/

what does the “&” after int and float, etc mean in void getInput, etc?

The "&" as part of a function parameter is a reference.

Without the reference symbol when you pass a value to a function it gets copied. Any changes you make to the value in the function isn't passed back to the value of the variable where you called the function.

https://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/

Passing by reference will allow the function to modify the passed variable directly, without the need to return a value.

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Using a return value allows you to pass back only one value. Since getInput() and heightCalculating() are modifying two variables values passing by reference allows for the two variables to be changed in the functions and those changes show up back in main(), or where the functions were called from.
Topic archived. No new replies allowed.