fscanf() not asking for input ?

This is a discussion on fscanf() not asking for input ? within the C Programming forums, part of the General Programming Boards category; Instead of asking me for the aperture value , the fprintf gets seemingly skipped , and the next thing that ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    59

    fscanf() not asking for input ?

    Instead of asking me for the aperture value , the fprintf gets seemingly skipped , and the next thing that prints is that parture = 0.0f , when it hasnt even prompted me for an input :
    Code:
    #include <stdio.h>
    int main(int argc, char * argv[])
    
    {
    
    
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    
    
    	int exposure=0;		
    	float aperture=0;	//Initiate the aperture variable - floating point number
    
    
    	fprintf(output,"\nEnter Exposure Time: ");	// Prompt the user for exposure time
    	fscanf(input,"&#37;5sd",&exposure);			// Store exposure time in the exposure variable
    							
    
    		fprintf(output,"Please enter the aperture size - like so : f1.2\n");	// Prompt the user for the aperture size.         			             
                    fscanf(input,"f%f",&aperture);    // Store the aperture value in the aperture variable. 
    		
    
    		
    		   if ((aperture==1.2) || (aperture==1.4) || (aperture==1.8) || (aperture==2.0)  || (aperture==2.8) || (aperture==4.0)  || (aperture==5.4) || (aperture==8.0) || (aperture==11.0) || (aperture==16.0)|| (aperture==22.0) || (aperture==32.0))
    	
    		{
    
    				fprintf(output,"You have entered a defined aperture.\n");
    				fprintf(output,"::::::Values::::::Entered::::::\n\n");
    					
    
    				fprintf(output,"The selected Exposure time is %d\n", exposure);         // Print the exposure time entered on screen
    				fprintf(output,"The selected Aperture is  : f%.1f\n",aperture);		// Print the aperture on screen
    		
                    }
    
    
    			
    				
                       else
    
    	        {
    					fprintf(output,"You have entered aperture value of f%.1f \n", aperture);
    					fprintf(output,"This is an invalid aperture, the program will now exit\n"); 								                    
    			                return 0;
    				
                    }
    
    
    }
    Last edited by ki113r; 09-07-2007 at 12:34 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well the first step is to learn how to indent code.
    Set your IDE to only use spaces for indenting, and set the tab stops to 4 spaces. If you use spaces and TABS, the result when posted on any forum is invariably a mess.

    > fscanf(input,"f%f",&aperture);
    Did you know this is part of the comment line?
    Your IDE has perhaps line wrapped this into another line, making you think it's OK. But it isn't.

    Consider this, your code reformatted
    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        // Mainline Variable Declarations
        FILE *output = stdout;
        FILE *input = stdin;
    
        int exposure = 0;
        float aperture = 0;         //Initiate the aperture variable - floating point number
    
        fprintf(output, "\nEnter Exposure Time: "); // Prompt the user for exposure time
        fscanf(input, "%5sd", &exposure); // Store exposure time in the exposure variable
    
        fprintf(output, "Please enter the aperture size - like so : f1.2\n"); // Prompt the user for the aperture size.
        fscanf(input,"f%f",&aperture);                                        // Store the aperture value in the aperture variable. 
    
        if ((aperture == 1.2) || (aperture == 1.4) || (aperture == 1.8)
            || (aperture == 2.0) || (aperture == 2.8) || (aperture == 4.0)
            || (aperture == 5.4) || (aperture == 8.0) || (aperture == 11.0)
            || (aperture == 16.0) || (aperture == 22.0) || (aperture == 32.0))
        {
            fprintf(output, "You have entered a defined aperture.\n");
            fprintf(output, "::::::Values::::::Entered::::::\n\n");
    
            fprintf(output, "The selected Exposure time is %d\n", exposure);  // Print the exposure time entered on screen
            fprintf(output, "The selected Aperture is  : f%.1f\n", aperture); // Print the aperture on screen
        }
        else
        {
            fprintf(output, "You have entered aperture value of f%.1f \n",
                    aperture);
            fprintf(output, "This is an invalid aperture, the program will now exit\n");  // Alert the user that incorrect value was entered.
            return 0;
        }
    }
    Perhaps move the return 0 to the actual end of the function.

    > fscanf(input, "%5sd", &exposure);
    What kind of format is that supposed to be?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function `main':
    foo.c:12: warning: char format, different type arg (arg 3)
    foo.c:35: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to make your floating point comparisons work OK, you REALLY NEED 1.2f, 2.8f, etc to make your compiler avoid converting your aperture to double, which causes it to have greater precision. Certain numbers (such as 1.4, 2.8, 5.6) can not be described EXACTLY in floating point (because they are 2x multiples of 0.7, which becomes 0.69999999...). When the float is converted to double precision it's just filled out with zero's. As you can perhaps understand, the value 0.69999990000 isn't exactly the same as 0.699999999999 - so the compiler says it's not the same).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21