C and float value problem

Do you have a technical question that doesn't really fit a specific console? Want some general info on electronics, hacking, making cookies, etc? Here's the place to ask! Go nuts.

Moderator:Moderators

Post Reply
Finmann
Posts:3
Joined:Sat Dec 03, 2011 8:23 pm
C and float value problem

Post by Finmann » Wed Feb 15, 2012 1:16 am

Hi first off here is my code (It's C, not C++, just C)

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int one;
  int two;
  int times;
  int sum;
  int diff;
  float quotient;
  char inString[81];
  printf("Input a value \n");
  gets(inString);
  one = atoi(inString);
  printf("Input a second value \n");
  gets(inString);
  two = atoi(inString);
  times = one*two;
  sum = one+two;
  diff = one-two;
  quotient = one/two;
  printf("Multiplied = %i \n Added = %i \n Subtracted = %i \n Divided = %f \n", times, sum, diff, quotient);
  system("PAUSE");	
  return 0;
}
The program is supposed to take in two real numbers and find their product, quotient, sum, and difference.
Ok, now everything works great, except for the quotient value, I'm using a float, as the result may be a fraction. However I don't get what I expect, it gives me a zero when I print the value.

I just can't figure out were I'm missing something.

User avatar
marshallh
Moderator
Posts:2986
Joined:Sat Sep 10, 2005 2:17 pm
360 GamerTag:marshallh
Location:here and there
Contact:

Re: C and float value problem

Post by marshallh » Sat Feb 18, 2012 10:20 pm

Code: Select all

    quotient = (float)one / (float)two;
Image

Finmann
Posts:3
Joined:Sat Dec 03, 2011 8:23 pm

Re: C and float value problem

Post by Finmann » Mon Feb 20, 2012 5:24 am

marshallh wrote:

Code: Select all

    quotient = (float)one / (float)two;
Ah, thx mate, that sorted it out.

Post Reply