LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2006, 09:51 PM   #1
Timberwolf753
LQ Newbie
 
Registered: Apr 2006
Location: Brick, NJ
Distribution: Slackware 13.0
Posts: 19

Rep: Reputation: 0
Need Assitence wiht rounding to nearest inch


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;

cout << "There are " << yards << " yard(s), "
<< feet << " feet(foot), "
<< inches << " inch(es)" << endl;

system("PAUSE");
return 0;
}
 
Old 10-12-2006, 02:30 AM   #2
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
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.

I hope this helps
--Ian
 
Old 10-12-2006, 09:55 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I don't mean to be a smart-ass, but did you not look for a rounding function... like... round()?

man 3 round

Also in math.h.
 
Old 10-12-2006, 10:44 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 10-12-2006, 11:39 AM   #5
Timberwolf753
LQ Newbie
 
Registered: Apr 2006
Location: Brick, NJ
Distribution: Slackware 13.0
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you for all the help guys got the program to work right. A+ here i come. lol
 
Old 10-12-2006, 05:34 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 10-12-2006, 06:14 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
umask taking the nearest even number anjanesh Linux - General 4 07-17-2006 12:12 PM
rounding a large double to 2 decimal places linuxmandrake Programming 2 03-17-2006 03:31 PM
C++ Rounding and Truncation Opeth Programming 4 09-17-2005 07:16 PM
Java Precission rounding. Tru_Messiah Programming 4 05-14-2004 11:23 PM
STUPID question on routing and my nearest computer kngharv Linux - Networking 1 07-12-2002 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration