trouble displaying floating-point value

Feb 27, 2010 at 12:33am
Hi. This program is suppose to accept your height in inches, and convert it to FEET with the remaining inches displayed also....

[CODE:]

#include <iostream>
using namespace std;

int main()
{ int variable;
int WO;
const int VOOB = 12;

cout << "What is your height in inches?____ \b\b\b\b\b";

cin >> variable;

int BO = variable / VOOB;
int ZO = variable % VOOB;
WO = ZO / VOOB;

cout << "Your equivalent height in feet is: " << BO << " feet, and " << ZO << "." << WO << " inches.\n";

return 0;
}

[/CODE]

Say you enter a non-integer number like, 65.5 inches, the output would be:

What is your height in inches?____
Your equivalent height in feet 5 feet and 5.0 inches.


The output in this case SHOULD be 5 feet and 5.417 inches.





Feb 27, 2010 at 12:51am
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited on Mar 4, 2010 at 9:58pm
Feb 27, 2010 at 1:24am
I know what the problem is.. I think

The modulus operator, % only works with whole numbers and NO floating point types... When trying to use 65.5, it is discarding the .5

I'll have to just think of another way of doing this...
Feb 27, 2010 at 1:43am
maybe something like this
1
2
int BO = variable / VOOB
double ZO =  variable / VOOB - BO


ZO would give you the decimal left over so you then convert from feet to inches.
Feb 27, 2010 at 2:01am
I don't understand


If BO = variable / VOOB

then subtracting BO from variable / VOOB would give me zero
Feb 27, 2010 at 2:02am
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited on Mar 4, 2010 at 9:58pm
Feb 27, 2010 at 2:10am
ahh, good point..


let me try this :)
Feb 27, 2010 at 3:00am
Got it working : )

Here is the new code:




#include <iostream>
using namespace std;

int main()
{
double variable;
const int VOOB = 12;

cout << "What is your height in inches?____ \b\b\b\b\b";

cin >> variable;

int BO = variable / VOOB;
double ZO = variable / VOOB - BO;
double FT_to_INCHES_CONVERSION = ZO * VOOB;



cout << "Your equivalent height in feet is: " << BO << " feet, and " << FT_to_INCHES_CONVERSION << " inches.\n";

return 0;
}


Also notice that I also had to change the int variable to double variable, for it to work...

You can now input a decimal number in inches, and it will output the number of FEET and exact number of inches left over..


thx, foobarbaz That was clever thinking

Topic archived. No new replies allowed.