I have spent a good 6 hours on this , yet it still doesn't work properly. When I compile it keeps spitting out 0 for both shutter and exposure values, plus it always gives me incorrect value for shutter speed. Please help !
[code]
#include %26lt;stdio.h%26gt;
int main(void)
{
int exposure=0;
float aperture=0;
int choice = 0;
printf("\nEnter Exposure Time: ");
scanf("%f",%26amp;exposure);
printf("The selected Exposure time is %f\n", exposure);
{
while (choice%26lt;1)
{
printf ("Please enter the aperture size ");
scanf("%d",%26amp;aperture);
if ((aperture==1.2) ||(aperture==1.4) ||(aperture==1.8) ||(aperture==2.0) ||(aperture==2.8) ||(aperture==2.8) ||(aperture==4.0) ||(aperture==5.6) ||
(aperture==8.0) ||(aperture==11)||(aperture==16)||(apert...
{
printf("You have entered a correct value ");
choice=2;
}
else
{
printf("Invalid aperture=%d\nPlease Try Again\n", aperture);
choice=1;
}
}
}
}
[/code]
Please check my code - something is wrong - C programming?
scanf("%f",%26amp;exposure) should use "%d" because you are dealing with an integer.
printf("The selected Exposure time is %f\n", exposure) should also use "%d" because exposure is an int.
printf("Invalid aperture=%d\nPlease Try Again\n", aperture) should be printf("Invalid aperture=%f\nPlease Try Again\n", aperture) because you are printing a float.
Also, when I ran your program and inspected the values of aperture (I entered 1.4 for example), the debugger showed me that aperture was actually 1.40000. That's why you aren't getting a match. You should either truncate the values or do something like aperture %26gt;=1.4 %26amp;%26amp; aperture %26lt;=1.5.
Using floating precision numbers in tests of equality makes things very difficult. Maybe you could make aperture an int and multiple your decimal values by 10.
For example, you are looking for 1.4 or 1.8. Accept 14 or 18 instead, then divide the result by 10 into a float to use in your actual calculations.
Example:
/*aperture is an int, not a float*/
scanf("%d",%26amp;aperture);
if ((aperture==12) ||(aperture==14) ||(aperture==18) ||(aperture==20) ||(aperture==28) ||(aperture==28) ||(aperture==40) ||(aperture==56) ||
(aperture==80) )
{
printf("You have entered a correct value ");
choice=2;
float ap=(float)aperture/10.0;
/*do calculations with ap*/
}
Reply:Erm.. I'm a noob at programming but this line
scanf("%f",%26amp;exposure);
doesn't look right to me. Your exposure variable is declared as an integer but the format descriptor %f is for a floating number. =\
Reply:YOU HAVE DECLARED EXPOSURE OF INT TYPE AND ACCEPTING THE VALUE AS %F AS FLOAT IN SCAN F
AND
YOU HAVE DECLARED APERTURE OF FLOAT TYPE AND ACCEPTING THE VALUE AS %D AS INT IN SCAN F
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment