Jump to content

Centimeters to Feet & Inches

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
vVR

vVR

    Newbie

  • Members
  • Pip
  • 6 posts
Hello,

This is my first post here. I found the forum while looking for a solution to my problem (below) but while I've not actually found the solution itself, I took a look around the site and it's a nice place you guys have, so I'd like to stick around.

But since I am here, I'd love if you could help me out.

I'm taking my first steps in C with C Primer Plus from Stephen Prata and so far I have enjoyed the book. I've come to an exercise question that I cannot solve though.

Here is the question:

Quote

Write a program that asks the user to enter a height in centimeters and then displays the height in centimeters and in feet and inches. Fractional centimeters and inches should be allowed, and the program should allow the user to continue entering heights until a nonpositive value is entered.

A sample run should look like this:

Enter a height in centimeters: 182

182.0 cm = 5 feet, 11.7 inches

Enter a height in centimeters (<=0 to quit): 168

168.0 cm = 5 feet, 6.1 inches

Enter a height in centimeters (<=0 to quit): 0

I can do the first bit, converting cm to feet easily enough, but the cm to inches is killing me. If anyone has the book, I'm on Chapter 5 (so you know how much I know and what I haven't gotten to yet).

#include <stdio.h>


const double cm_to_feet = 30.48;

const double cm_to_inch = 2.54; // (0.39);


int main(void)

{

	float cm, inches;

	int feet;


	printf("Enter a height in centimeters (<=0 to quit): ");

	scanf("%f", &cm);


	while(cm > 0)

	{

		

		feet = cm / cm_to_feet;;

		inches = (int) cm % (int) cm_to_inch;

		

		printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inches);

	

		printf("Enter a height in centimeters (<=0 to quit): ");

		scanf("%f", &cm);

	}


	printf("Done!\n");


	return 0;

}

Obviously the above doesn't work because you cannot use the modulus operator with a float (or if you can, I've not got to it yet!). Doing something like:

inches = cm / cm_to_inch;

doesn't work either. So I'm lost.

I know asking for help on a book exercise is odd because you don't know what I do know or what I don't know. The above is a pretty thick portion of what I do know though. The only things I also know about (but haven't mentoned) are sizeof and size_t and creating functions with arguments.

Thanks for reading :)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
inches = inches - feet*12;
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Try this .... Hope this will work...
#include <stdio.h>


const double cm_to_inch = 2.54; // (0.39);


int main(void)

{

        float cm, inches;

        int feet;


                printf("Enter a height in centimeters (<=0 to quit): ");

                scanf("%f", &cm);


        while(cm > 0) {


                inches = cm / cm_to_inch;;

                feet = inches/12;

                inches = inches-(feet*12);


                printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inches);

                printf("Enter a height in centimeters (<=0 to quit): ");

                scanf("%f", &cm);


        }


        printf("Done!\n");


        return 0;

}



#4
vVR

vVR

    Newbie

  • Members
  • Pip
  • 6 posts
That did the trick!

Thank you so much. That was a massive help :)