-
true division in C ?
Hi there!
This is my first post and I'm a complete noob to C, so
please bare with me.
I'm having an awful time trying to do true division.
All my variables are int type but my result comes out to 0.
I tried changing one of the variables to double but i'm getting compilation errors.
Here is the original code:
Code:
#include <stdio.h>
int main()
{
int resgallons;
int currentppm;
int targetppm;
int result
printf( "Enter a 2 digit number: How many gallons are in you res?\n" );
scanf("%d", &resgallons);
printf("Enter you current PPM level.\n");
scanf("%d", ¤tppm);
printf("Now enter your desired PPM level\n");
scanf("%d", &targetppm);
result = ( targetppm - currentppm) / targetppm * 8 * resgallons);
printf("%d", result);
return 0;
}
Then I tried this:
Code:
#include <stdio.h>
int main()
{
int resgallons;
int currentppm;
int targetppm;
int result
printf( "Enter a 2 digit number: How many gallons are in you res?\n" );
scanf("%d", &resgallons);
printf("Enter you current PPM level.\n");
scanf("%d", ¤tppm);
printf("Now enter your desired PPM level\n");
scanf("%d", &targetppm);
double result = ( (double)targetppm - currentppm) / targetppm * 8 * resgallons);
printf("%lf", result);
return 0;
}
But this gave me compilation errors.
Could someone point me in the right direction.
-
You forgot a ; after
Also, you first declare result to be an int, and then proceed and redeclare it to be a double.
-
In both of your codes you are also missing a bracket in the "result=" line. Other errors are as Memloop pointed out.
-
AHA! I knew it.
Complete noob mistakes.
Thanks for the help guys.
Everything is cool now.