Basic math brainfart

Discuss all aspects of programming here.

Moderator: The Mod Squad

Basic math brainfart

Postby Falklian » Mon Aug 01, 2005 4:06 pm

Ok, I'm trying to brush up on my C++ (haven't touched it in over 5 years) not for anything in particular, just for my own enjoyment. Well, I haven't taken a math class in 5+ years as well (got up to calculus) and this is probably a really simple question with a very simple answer and for the life of me, I can't figure it out. I'm reading a book and one of the exercises asks for me to convert inches to feet and inches and to use a const as the conversion factor. I can't figure out the conversion factor.

Some (pseudo)code:

Code: Select all
const conversion_factor_I_can't_figure_out

//now this is what *I* want to do, just generic code, not what I'm really gonna do ;)

int totalInches = 75; //(6 foot 3 inches)
int feet = totalInches / 12; //drops the decimal of .25 and feet comes to 6
int inches = totalInches % 12; //modular, drops the remainder of 3 into inches

Maybe I'm going about this all wrong, but to me, what I typed in would be correct and maybe the author just wants something simple like:

Code: Select all
const int ONEFOOT = 12;

I'm thinking that a const cannot be a function or a formula. That would better be handled by a #define but then again I haven't been familiar with C++ for quite a while.

I think what I'm doing is right, but if someone wants to correct me, feel free, it's what I'm looking for :wink:

I can post the question too if need be.
Black Belt
Black Belt
Posts: 921
Joined: Tue Oct 01, 2002 2:06 pm
Location: Illinois

Postby Roach412 » Mon Aug 01, 2005 4:20 pm

the way you're doing it is entirely correct. that should work for ya.

still need:
Code: Select all
int total = feet + inches;

a const cannot be a function or formula (as i can remember....).

moving into defines and such seems a little bit over-the-top IMHO.

Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
Logitech G402 Hyperion Fury
Black Belt 2nd Degree
Black Belt 2nd Degree
Posts: 2738
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest