-
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);
}
}
-
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.
-
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???
-
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:
-
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 ??
-
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.
-
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");
}
}
-
one for the null terminating value.
-
> 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.