TLEJ Posted January 21, 2012 Report Share Posted January 21, 2012 I have a very very basic C++ problem... Here's my code so far: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { /* Initiate Variables */ int miles, gallons, mpg; /* Get Vaules for Variables */ printf("Enter miles driven: "); scanf_s ("%d", &miles); printf("Enter gallons used: "); scanf_s ("%d", &gallons); /* Calculate MPG, then output */ mpg = gallons / miles; printf("Your miles per gallon was: %d\n", mpg); return 0; } But if ran, I get 0 unless miles = gallons, I get 1. It's doing a "trancauted 0?" or something.. Idk. I need help.. I forgot how to get this to run right. Quote Link to comment Share on other sites More sharing options...
MrBalll Posted January 21, 2012 Report Share Posted January 21, 2012 I haven't done c++ in awhile, but I think because you have the values initiated as 'int' it won't display decimals. So, you have it set up to divide miles by gallons, which gives you less than 1, ergo it displays 0. So I would try switching it to mpg = miles / gallons and see what that gives you. That should fix it. Quote Link to comment Share on other sites More sharing options...
MrBalll Posted January 21, 2012 Report Share Posted January 21, 2012 (edited) Whoops, double post. Edited January 21, 2012 by MrBalll Quote Link to comment Share on other sites More sharing options...
TLEJ Posted January 21, 2012 Author Report Share Posted January 21, 2012 (edited) Oh wow I am a DA... I don't even know how I typed those backwards.. Works like a charm now. EDIT: O DAM MR BALL IS THAT U MY NIGGA? HALOMODS? DAT U? Edited January 21, 2012 by TLEJ Quote Link to comment Share on other sites More sharing options...
MrBalll Posted January 21, 2012 Report Share Posted January 21, 2012 Hehe, sure is. Yeah, I wasn't sure if C++ did the math backwards or not. Like divided gallons by miles. Glad that helped you. Quote Link to comment Share on other sites More sharing options...
TLEJ Posted January 21, 2012 Author Report Share Posted January 21, 2012 BEEN 2 LONG. WERE TURAL @??? And yeah, I think that's why I ended up typing them backwards.. I originally had miles/gallons=mpg in my head, and I knew I had to switch them around, so I guess I switched everything.. Quote Link to comment Share on other sites More sharing options...
MrBalll Posted January 21, 2012 Report Share Posted January 21, 2012 Who knows. Probably off moderating and saying no pix no pruf on some forum somewhere. Yeah, going from thought to keyboard changes sometimes. At least it's all working now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.