-
bad io output
The output I get from this program just shows all zeros, I cant figure out what is wrong as this is how the scanf and printf statements are formatted in the book. Any help please?
Code:
#include <stdio.h>
double min(double, double);
int main(void)
{
double first, second, displaylow;
printf("Please enter 2 floating point numbers: ");
scanf("%f %f", &first, &second);
displaylow = min(first, second);
printf("%f is the lower of the floats entered", displaylow);
return 0;
}
double min(double one, double two)
{
if (one > two)
return two;
else
return one;
}
-
change
Code:
scanf("%f %f", &first, &second);
to
Code:
scanf("%lf %lf", &first, &second);
At least that's what gcc says.
-
carrotcake1029- Thanks, that fixes the problem on this program. However I tried this fix on my other program that had the same problem and it didnt work. Could I bother for a tad more help?
Code:
#include <stdio.h>
void larger_of(double *, double *);
int main(void)
{
double first, second;
printf("Please enter 2 floating point numbers: ");
scanf("%lf %lf", first, second);
larger_of(&first, &second);
printf("The values entered are now %f and %f", first, second);
return 0;
}
void larger_of(double * one, double * two)
{
if (one > two)
*two = *one;
else
*one = *two;
}
-
My guess would be that:
is incorrect. You are comparing the address of one with the address of two.
Also, if "one" is greater than "two", you probably need to SWAP the two values, which involves storing one value in a temporary variable, the copying the second value into the one value, and then storing the temporary in the second variable.
--
Mats
-
aarrgh *facepalms* thx matsp