Thread: C error please help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    C error please help

    This is the code that i am working on and it compiles and runs but when i enter the f2 value it says wrong value.can some one tell me what im doing wrong? ta
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    // This program gets Exposure time and Aperture and outputs the exposure time and the correct Aperture value. 
    
    
    int main (int argc, char * argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    
    
    double exposure;
    char *aperture;
    aperture=0
        
     //prompt user for Exposure time.
         fprintf(output,"Please Enter the exposure time in seconds: ");fflush(output);
    
         //input Exposure time.
         fscanf(input,"%lf",&exposure) ;fflush(output);
    
         fprintf(output,"Thank you\n");
              
    
         //prompt user for Aperture .
         fprintf(output,"Please Enter the aperture value,it must be f2: ");fflush(output);
    
         //input Aperture.
         fscanf(input,"%s",&aperture) ;
    
    
    		if (strcmp(aperture,"f2")==0)
                   
    {
    		// Output Results
    		fprintf(output,"--> The Exposure time you entered is: %lf\n",exposure);                               
      		fprintf(output,"-->The Aperture value you entered is acceptable and is:%s\n",aperture); 
    }
    else 
    {   
     		fprintf(output,"The aperture you entered was not valid ,the program will exit now \n");fflush(output);
    } 
     
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Your pointer isn't pointing anywhere.
    2. You're using fscanf() wrong for the type of variable you're attempting to scan to.

    Which compiler are you using?

    You can add "poor indentation" as well.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    what do you mean by pointer (new to C)
    what do i use instead of fscanf

    its in a unix environment and we use cc FILENAME.c so im guessing its a gcc compiler???

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    char *aperture;
    this is a pointer. you need an array, so that you can actually store the string somewhere. the pointer just points to somewhere that holds a string, but since you don't assign a value to it, it just has a random value.

    make the array at least the number of characters to store, plus one.

    for example:
    Code:
    char aperture[15];
    Last edited by robwhit; 09-13-2007 at 12:00 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    my mistake, it doesnt even compile. it gives the following error :
    camera_test.c: In function ‘main’:
    camera_test.c:21: error: expected ‘;’ before ‘fprintf’

    do i need to use sscanf ??

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    aperture=0;

    //prompt user for Exposure time.
    fprintf(output,

    I guess "error: expected ‘;’ before ‘fprintf’" means you need big red there.
    Seems to fit the "before" aspect of the error message.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    oright thanks guys i changed it to the following code and compiles and runs fine. ta
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    // This program gets Exposure time and Aperture and outputs the exposure time and the correct Aperture value. 
    
    
    int main (int argc, char**argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    
    
    double exposure;
    char aperture[4];
    
        
     //prompt user for Exposure time.
         fprintf(output,"Please Enter the exposure time in seconds: ");
    
         //input Exposure time.
         fscanf(input,"%lf",&exposure) ;
    
         fprintf(output,"Thank you\n");
              
    
         //prompt user for Aperture .
         fprintf(output,"Please Enter the aperture value");
    
         //input Aperture.
         fscanf(input,"%s",&aperture) ;
    
    
    		if (strcmp(aperture,"f1.2")==0 || strcmp(aperture,"f1.4")==0 || strcmp(aperture,"f1.8")==0 || strcmp(aperture,"f2")==0 || 					strcmp(aperture,"f2.8")==0 || strcmp(aperture,"f4")==0 || strcmp(aperture,"f5.6")==0 || strcmp(aperture,"f8")==0 || 						strcmp(aperture,"f11")==0 || strcmp(aperture,"f16")==0 ||strcmp(aperture,"f22")==0 || strcmp(aperture,"f32")==0)
                   
    {
    		// Output Results
    		fprintf(output,"--> The Exposure time you entered is: %g\n",exposure);                               
      		fprintf(output,"-->The Aperture value you entered is acceptable and is:%s\n",aperture); 
    }
    else 
    {   
     		fprintf(output,"The aperture you entered was not valid ,the program will exit now \n");
    } 
     
    
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    char aperture[5];
    one for the null terminating value.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > its in a unix environment and we use cc FILENAME.c so im guessing its a gcc compiler???
    Could be, in which case, try

    cc -W -Wall -ansi -pedantic -O2 FILENAME.c

    This will catch a lot of problems, in particular printf/scanf format string/parameter mis-matches.

    In particular, drop the & from this line.
    fscanf(input,"%s",&aperture) ;


    cc -v
    cc --version
    May tell you more.
    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.

Popular pages Recent additions subscribe to a feed