473,671 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hey,n00b question converting cm to inches,then yards,feet,inch es

3 New Member
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming

here it is

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{ int remainder,remai nder2,yards,fee t,inches;

float centi,inche5;

cout<<"What is the length in Centimeters"<<e ndl;

cin>>centi;

//convert to nearest inch

inche5=centi/2.54;

inches=static_c ast<int>(inche5 );

//Compute Yards

yards=inches/36;

//Drop Remainder To Feet

remainder=inche s%36;

//Compute Feet

feet=remainder/12;

//Drop Remainder To Inches

remainder2=rema inder%12;



cout<<"Yards="< <yards<<endl;

cout<<"Feet="<< feet<<endl;

cout<<"Inches=" <<remainder2<<e ndl;



system("pause") ;

return 0;

}


i used static_cast to convert float to int without runtime errors, the problem is that static_cast rounded down so the output tended to be 1 less inch then what's mathmatically correct...is there a better way to convert float to int or somehow name all variables as int but still keep the conversion of cm/2.54?

thanks for all your help
Sep 23 '06 #1
4 10133
onefry
3 New Member
also.. basically what i must do.. is convert cm to inches... round to the nearest inch, then my output must be in yards, feet, inches i figured that i could just use int as in (int)inches=(in t)cm/2.54 but since i'm / an int by not an int it gives a runtime error
thanks for all your help once again
Sep 23 '06 #2
onefry
3 New Member
bump...please if anyone can give me any tips they are greatly appreciated!
Sep 23 '06 #3
D_C
293 Contributor
Tip: Don't re-invent the wheel.
Sep 24 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
D_C has a good point but on the subject of converting float to int then

assuming that the value of the float is in range for the int then you will always loose the decimal places. When you put your conversion in place you will have to decided if that is ok if it isn't then you will need to do something to correct the conversion.

You instance in your case (converting cm to inches) you may decided hat you actually want the value to the nearest inch. This means that for decimals >= x.5 you want to round up to (x+1) and for values < x.5 round down to x.

inches = (int)inche5;

rounds downall fractions but you can achieve the nearest inch this way

inches = (int)(inche5+0. 5);

This works because for values of inche5 >= x.5 then inche5 + 0.5 is >= x+1 so inches takes the value x+1. simialrly for values <x.5 inche5 + 0.5 > x+1 so inches takes the value x.
Sep 24 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
17305
by: Brent Hoskisson | last post by:
What is a unit of height and width of a form measured as (pixels?) and how can I convert that measurement to inches? Thanks Brent
1
5038
by: SpookyTooth | last post by:
I am trying to write a program that will convert a height of a person from feet and inches to centimeters. I want to use the relationship: 1 foot = 30.48 cm and 1 inch = 2.54 cm. I am trying to get the output in a form of a table where the left column would contain the height of the person in feet and inches from like 4'5" to 6'5" (with an increment of 1 inch), and the right column will contain the corresponding height in centimeters. The...
3
25907
by: SpookyTooth | last post by:
I am trying to write a program that will convert a height of a person from feet and inches to centimeters. I want to use the relationship: 1 foot = 30.48 cm and 1 inch = 2.54 cm. I am trying to get the output in a form of a table where the left column would contain the height of the person in feet and inches from like 4'5" to 6'5" (with an increment of 1 inch), and the right column will contain the corresponding height in centimeters. The...
1
4161
by: printline | last post by:
Hello I need a javascript that converts mm to inches. Ex. I have a drop down menu in my form where users can choose either mm or inches. If they choose inches for example i have two text fields that should convert the typed in number to inches and a drop down meny with four values, that also should be converted to inches..... Can someone please help me........?
4
12226
by: keith | last post by:
I am writing a simple program to convert mm to feet and inches. I have it as follows: #include <stdio.h> #include <math.h> void main() { int x=11; float mm=0; float answer=0;
6
3866
by: bratiskovci | last post by:
1. How do I change the program so that the program does not terminate after completing one conversion. Instead, the program should continue to convert values until the user indicates that he/she wishes to quit. 2. How do you change the structure of the program so that ALL conversion computations are in a function of their own. Input: the user inputs the amount in one of the following forms: # C value # F value # ...
6
12924
by: philiprodley | last post by:
Have 3 text boxes. Feet, inches and metres. As I type in feet then inches, I would like to convert this to metres and place the result in the metres text box as soon as I tab on to the next field. Can anyone help, please?
3
1454
by: corky20 | last post by:
Problem: The Sports Studies Department of the Institute would like to produce statistics on the Belfast and London marathons in 2006. In particular, they want to compare the times of a selected group of ten runners from different countries in the United Kingdom who competed in each marathon. You are required to write a program to read each runners’ time to finish each marathon and to produce the following statistics • List of names of...
3
3890
by: socondc22 | last post by:
Im stuck at this point. The way i was trying to do this was running if statement around my void function and then a else if statement around a different void function but it didnt work... right now i deleted part of what i had but left my skeleton of it there... #include <iostream> using namespace std; void get_numbers(double& input1, double& input2); void show_results(double output1, double output2); int main() { char letter;
0
8476
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7433
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6223
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5695
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.