Hello everyone, I'm new to programming and don't really know how everything works so far, so please don't laugh at me. Well. I'm trying to create a program that would add feet and inches. I already started writing it, however can't understand the basics, how to come up with the function. Please help me out with your suggestions. Thanks in advance
Your program logic seems to be a little messed up here. I'm not sure I entirely understand what you're trying to do here, but I think you mean if your input was:
1 2
5 6
5 2
Then the output would be:
feet = 10 inches = 8
In that case, you're going to want to:
1 2 3 4
1) add up ft_1 and ft_2
2) add up in_1 and in_2
3) if in_2 and in_2 are greater than 12, increment the sum of ft_1 and ft_2 by one and subtract 12 from the sum of in_1 and in_2
4) output your results
Thanks again guys, I owe you for this one. I've fixed my code so everything is working fine now. Another question is what should I do to get a result in fraction (with decimal point) ? Any suggestions ?
I realize everything is fine, but you have an unnecessary line of code, and unnecessary lines are usually a bad thing. There's no need for it, so it's worthwhile to cut it out.
And again, what do you mean a result in decimals? Give an example, please.
If you just want inches to be able to have a decimal, research the float and double data types.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main ( void )
{
int feet, inches, ft_1, in_1, ft_2, in_2, temp_in;
cout <<"enter feet and inches"<<endl;
cin>>ft_1>>in_1;
cout <<"enter feet and inches"<<endl;
cin>>ft_2>>in_2;
feet = ft_1+ft_2;
inches = in_1 + in_2;
temp_in = inches % 12;
feet += (inches - temp_in) / 12;
cout<<"feet="<<feet<<"inches="<<temp_in<<endl;
system ( "pause" );
return 0;
}
Line 19 was deleted and line 21 changed.
Line 19 is unnecessary in this case, but if you will like to do more coding after it and using this results it is line 19 lets to make code more clear.