ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello all C++ noob here. Ok so here is my little problem. I am converting centimeters to inches(To the nearest inch) but I do not know how to do it. I have tried but so far no luck. Any help would be appriates as its done this Friday. Here is my source. This is do wiht Dev-C++.
// Take a number in Centimeters and convert it to yards, feet, and inches
#include <iostream>
using namespace std;
// Declare constant variables
const int yard = 36;
const int foot = 12;
const double centimeters_to_inches = 0.393700787;
int main()
{
// Declare some varibales
double centimeters;
int yards, feet, inches, total_inches;
// Ask for a number in centimeters
cout << "Please enter a number in centimeters: ";
cin >> centimeters;
// Convert centimeters to inches
total_inches = centimeters * centimeters_to_inches;
// Calculate how many yards are in the total inches
yards = total_inches / yard;
// Find the the remainder of yards
total_inches = (total_inches % yard);
// Calculate how many feet are in the remainder
feet = total_inches / foot;
// Find the reaminder of feet
total_inches = (total_inches % foot);
// Calulate how many inches are in the remainder
inches = total_inches;
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088
Rep:
Do you want to round down, or round up?
Look at the floor() and ceil() functions in math.h.
floor( totalInches) will return the largest integer that is smaller than totalInches.
ceil( totalInches) will return the smallest integer that is larger than totalInches.
And be sure to apply any of those functions to the centimeters * centimeters_to_inches calculation, not the total_inches value, since that value has been declared as an int, and will have been truncated to an int by the automatic type conversion. (You may, in fact, get a compiler warning for setting an int to a double.)
<Edit>You might also want to see if your compiler supports the decimal data type.</Edit>
Last edited by PTrenholme; 10-12-2006 at 10:51 AM.
Hey, I thought the guidelines here said "No homework problems!"
The point of the homework is to teach you how to find the answers on your own, in the instructional materials you should be studying. And, of course, the point in studying is to learn everything else available in you study material.
Hey, I thought the guidelines here said "No homework problems!"
The point of the homework is to teach you how to find the answers on your own, in the instructional materials you should be studying. And, of course, the point in studying is to learn everything else available in you study material.
nope they say
Quote:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
All I see in this thread is help/hints not solutions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.