c - Unused Variable Error -


i new programming , learning c language. code. have tried different fixes, such making sure have semicolons. however, when go check code in online compiler, unused variable , data definition has no type or storage class. information me, assure you, please let me know think possible solution be.

//c code //this program calculate average speed of passing cars using while-end loop        //developer: jasmine tucker //date: 7 sept 2014 #include <stdio.h> int main () {     /* define variable */     int average_speed;     int car_count;     int car_speed; }; /* set variable values */ average_speed= 0; car_count=0; car_speed=0;  /*while-end loop */ while (car_count > 0) {     printf (“enter car’s speed %s:”, car_count);     scanf (“%d”, & car_speed);     average_speed= car_speed/ car_count     car_count++; } printf (“\n average speed %d miles per hour. \n”, (average_speed)); return 0;  } 

a few things:

int main() 

should be

int main() 

this perhaps easy typo, or unfortunate side effect of grammar check.

you studying standard types in c. modifiers aside, there not many, , except special types, _bool, _complex, _imaginary, lowercase. (the same holds true keywords). storage class refers less commonly used, or @ least out of scope of program (auto,register,static,extern).

the following definitions use int type well, reproduce them here [sic].

/* define variable */ int average_speed; int car_count; int car_speed; }; /* set variable values */ average_speed= 0; car_count=0; car_speed=0; 

as others have mentioned, there extraneous curly brace after 3 variables declared. }; (notice how sad looks.) if coming language requires semi-colons after curly braces, have hard habits break.

in case, commenting out should remove several errors:

/* }; */ 

as closing block main().


as user haini pointed out, pull variable definitions outside of main(), allowing them accessible function. (use across source files bring aforementioned extern). programmers use special varaible [pre|suf]fixes distinguish global local variables.

int g_average_speed; int g_car_count; int g_car_speed; 

as these variables need initialized before use, can in definition:

int g_average_speed = 0; int g_car_count = 0; int g_car_speed = 0; 

often, use of global variables discouraged, preferring instead parameter-based sharing of variables, makes more sense once introduce multiple functions.


as mentioned, 'while' not keyword, while while is. variable types, list of keywords short, , mostly lowercase. know keywords avoid using them variable/function naming.

as far logic concerned, while-loop never begin, expression car_count > 0 not satisfied you've initialised car_count 0.

while it's easy hard-code value, may want set variable such max_cars upper limit , check car_count < max_cars. (don't forget you're counting 0).

/* define variable */ int max_cars = 10; /* rest of variables, init */ while( car_count < max_cars ) 

now, aside interesting quotations '“' give trouble, , missing semicolon @ average_speed = car_speed / car_count pointed out again haini, should try step through loop mentally. don't ever forget users inherently evil , attempt possibly unforseen values when allowed interact program (scanf()). negative values , 0 not out of question int , %d, though may expect cars 'parked' , speed 0. down line, unsigned modifier , %u may of use.

in case, it's in habit of sanitizing user input, and/or giving user option opt-out (i.e. "type -1 break..." ), , checking invalid or exit codes if. (break may keyword pursue in case)

your average_speed calculation doesn't quite seem right. don't forget you're storing values integers, you're gonna have rounding errors.

first think happens when initial case arrives -- car_count, , happens when divide value?

then, think final case, (assuming upper boundary 10) in car_count == 9. assigning car_speed / car_count average_speed. want?

you can minimize rounding errors , more difficult calculation maybe 'keeping track' of total of speeds, , 1 average calculation.

/* define variable */ int total_speed = 0; 

in while loop:

total_speed += car_speed; 

or

total_speed = total_speed + car_speed; 

and outside of loop:

average_speed = total_speed / (car_count - 1); 

(the adjustment car_count necessary because value increments after final loop.) note: in limited example, average_speed variable may not necessary, unless used outside of printf().


Comments