Custom Function Question

Hey y'all,

I'm wondering if there is a way I can combine the two Convert functions into one. So for instance instead of having a ConvertFt and ConvertIn function, just having one Convert function. Thanks!

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


void Input(int& Feet, int& Inches);
double ConvertFt(int Feet);
double ConvertIn(int Inhces);
void Output(double Meters, double Centimeters, int Feet, int Inches);



int main()
{
	int Feet, Inches;
	double Meters, Centimeters;
	char ans;
	do
	{
		Input(Feet, Inches);
		Meters = ConvertFt(Feet);
		Centimeters = ConvertIn(Inches);
		Output(Meters, Centimeters, Feet, Inches);
		
		std::cout << "Would you like to run the program again? [Y/N]\n";
		std::cin >> ans;
	} while (ans == 'y' && 'Y');
}

void Input(int& Feet, int& Inches)
{
	std::cout << "Please enter the number of feet as a real whole number\n";
	std::cin >> Feet;
	std::cout << "You have entered " << Feet << "ft\n";

	std::cout << "Please enter the number of Inches as a real whole number\n";
	std::cin >> Inches;
	std::cout << "You have entered " << Inches << "in\n";

}


double ConvertFt(int Feet)
{
	return Feet * 0.3048;
}

double ConvertIn(int Inches)
{
	return Inches * 2.54;
}


void Output(double Meters, double Centimeters, int Feet, int Inches)
{
	std::cout << Feet << " Feet converted to Meters is " << Meters << std::endl;
	std::cout << Inches << " Inches converted to Centimeters is " << Centimeters << std::endl;
}
depends on what you mean and want.
you can do this:

double convert (double input, char type)
{
if(type == 'f') return input* 0.3048;
else return input * 2.54;
}

what I am saying is that the code can't tell that what you handed it is in inches, feet, or furlongs. Its just a number. To get the conversion factor right you have to know what it is, and that means having its units ride along with it. You had that implicitly before by calling the right function, but to roll them into one, it now has to be explicit.

Another way to do it would be to not have functions at all.

enum units {inches,feet, meters, etc, maxunits}
double convert[maxunits] = {0.3048,2.54,etc...}; //values match position above, so first one is for inches, second for feet, ... etc
result = input*convert[inches];

I highly recommend you make the conversion factors have both sides. that is name them inches_to_meters or meters_to_furlongs so you know exactly what you have and what you get.
Last edited on
@jonin
I see what you are saying in the first part. I am defining it as a function because that is what is asked of me in the HW problem, I should have specified that. But I do understand what you mean by the if else statement, and differentiating between the two by changing their data types. Thanks!
while (ans == 'y' && 'Y');
(1) A character cannot be both y and Y at the same time
(2) Each statement needs to be independent
1
2
3
do {
    // ...
} while (ans == 'y' || ans == 'Y');
Last edited on
@Ganado

Whenever I input it as an or statement it doesn't seem to work. For instance i'll type in 'n' and it will just run the program again. The compiler gives me this error "(<expression> || <non-zero constant>) is always a non-zero constant."

Edit: Nevermind I figured it out, thanks for the clarification bro.
Last edited on
Show code that doesn't compile, and we can tell you what you're doing wrong.

error "(<expression> || <non-zero constant>) is always a non-zero constant."
Again, each statement needs to be independent or stand on its own. You can't just have 'Y' as a statement, you need to have ans == 'Y'.
Last edited on
@Ganando

Yeah I forgot the "ans == 'Y'" part.
ty.
Last edited on
fyi:
if(ans == 'y' || 'Y')
says
if ans == y or true, because 'Y' is an integer, and its not zero, to the compiler.
(x) or (true) is just (x), the or true part is redundant and ignored.
this is why it will compile and not work, even though it is not remotely like what you intended.
Hello noblemin,

As jonnin first said "depends on what you mean and want."

You could keep it simple with:
1
2
3
4
5
// <--- Function call.
Convert(Feet, Inches, Meters, Centimeters);

// <--- Function definition.
void Convert(int Feet, int Inches, double& Meters, double& Centimeters)


I try to add to what jonnin said.

while (ans == 'y' && 'Y');. If "y" was entered then the lhs of && would be true and the rhs is always true because the value of "Y" is not zero. So the expression evaluates to true. If "ans" has anything other than "y" the lhs is false and as a logical "AND" the rhs is never evaluated because the lhs is false and the whole can never evaluate to true.

In something like this I have always seen and used while (ans == 'y' || ans == 'Y'); to test for either case. As a logical "OR" only one side has to be true for the whole to be true.

An alternative would be while (std::toupper(ans)== 'Y'); // Requires header file "cctype". Or you could use "tolower" to change the case. With either function if the case does not need to be changed nothing happens. Also if "ans" does not contain a letter nothing happens.

Hope that helps,

Andy
Topic archived. No new replies allowed.