Jump to content

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Who's Online   1 Member, 0 Anonymous, 710 Guests (See full list)

×
×
  • Create New...