-2

I am new to programming and I am currently learning the C language. This is my code. I have tried different fixes, such as making sure I have my semicolons. However, when I go to check my code in the online compiler, I always get unused variable and data definition has no type or storage class. Any information would help me, I assure you, so please let me know what you think a possible solution could be.

//C code
//This program will calculate the average speed of passing cars using a 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 the car’s speed %s:”, car_count);
    scanf (“%d”, & car_speed);
    average_speed= car_speed/ car_count
    car_count++;
}
printf (“\n The average speed is %d miles per hour. \n”, (average_speed));
return 0; 
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
jastuck1
  • 19
  • 1
  • 1
  • 1
    Check your course notes. What is `WHILE`? – Jongware Sep 09 '14 at 19:09
  • 1
    C is case-sensitive. `int` not `Int`, `while` not `WHILE`. Also proper indentation would make it clear that you have mismatched curly braces. – FatalError Sep 09 '14 at 19:10
  • Why does your main function end after the 3 variable definitions? – Borgleader Sep 09 '14 at 19:10
  • ^^ and it has a trailing semi- which you wouldn't want anyways – polarysekt Sep 09 '14 at 19:11
  • Firstly, there's no such thing as "Unused Variable Error" in C language. A similar message can be generated by some compilers as a warning, not an error. Secondly, the code you posted contains a massive amount of errors completely unrelated to any "unused variables". Apparently, you posted fake code. Post real code and provide a meaningful description of the problem. – AnT stands with Russia Sep 09 '14 at 19:12
  • 1
    The only possible solution that I see is for you to go back to your book / course notes and learn the basics of `c` syntax. – PM 77-1 Sep 09 '14 at 19:17

3 Answers3

3

A Few Things:

Int main()

should be

int main()

This is perhaps an easy typo, or the unfortunate side effect of a grammar check.

You could probably do well by studying the standard types in C. Modifiers aside, there are not very many, and except for special types, _Bool, _Complex, _Imaginary, they are lowercase. (The same holds true for keywords). Storage class refers to something less commonly used, or at least out of the scope of this program (auto,register,static,extern).

The following definitions use the int type as well, so I will 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 is an extraneous curly brace after the three variables are declared. }; (Notice how sad he looks.) If you are coming from a language that requires semi-colons after curly braces, you have some hard habits to break.

In any case, commenting that out should remove several errors:

/* }; */

as this is effectively closing the block for main().


As user haini pointed out, you could actually pull the variable definitions outside of main(), allowing them to be accessible to any function. (Use across source files would bring up the aforementioned extern). Some programmers use special varaible [pre|suf]fixes to distinguish global from local variables.

int g_average_speed;
int g_car_count;
int g_car_speed;

As these variables need to be initialized before use, you can do this in the definition:

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

Often, the use of global variables is discouraged, preferring instead parameter-based sharing of variables, which makes more sense once you introduce multiple functions.


As mentioned, 'WHILE' is not a keyword, while while is. As with the variable types, the list of keywords is very short, and mostly lowercase. It is good to know the keywords so as to avoid using them for variable/function naming.

As far as logic is concerned, your while-loop will never begin, as the expression car_count > 0 will not be satisfied as you've initialised car_count to 0.

While it's easy to hard-code a value, you may probably want to set another variable such as max_cars to an upper limit and check for car_count < max_cars. (Don't forget you're counting from 0).

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

Now, aside from the interesting quotations '“' which will give you trouble, and the missing semicolon at average_speed = car_speed / car_count as pointed out again by haini, you should try to step through your loop mentally. Don't ever forget that users are inherently evil and will attempt possibly unforseen values when allowed to interact with the program (scanf()). Negative values and 0 are not out of the question with int and %d, though you may expect some cars to be 'parked' and thus speed 0. Down the line, the unsigned modifier and %u may be of use.

In any case, it's good to get in the habit of sanitizing user input, and/or giving the user an option to opt-out (i.e. "TYPE -1 to break..." ), and checking for invalid or exit codes with an if. (break may be the keyword to pursue in this case)

Your average_speed calculation doesn't quite seem right. Don't forget you're storing values into integers, so you're gonna have some rounding errors.

First think about what happens when your initial case arrives -- what is car_count, and what happens when you divide by that value?

Then, think about the final case, (assuming your upper boundary is 10) in which car_count == 9. You will be assigning car_speed / car_count to average_speed. Is this really what you want?

You can minimize rounding errors and more difficult calculation by maybe 'keeping track' of the total of the speeds, and only one average calculation.

/* define variable */
int total_speed = 0;

In your while loop:

total_speed += car_speed;

or

total_speed = total_speed + car_speed;

And then outside of the loop:

average_speed = total_speed / (car_count - 1);

(The adjustment to car_count is necessary because the value increments after the final loop.) NOTE: in this limited example, the average_speed variable may not be necessary, unless used outside of the printf().

Community
  • 1
  • 1
polarysekt
  • 562
  • 1
  • 5
  • 17
1

There are a few issues in your code that I see.

  1. The code will never get in the while loop. If you initialize it to 0, it will never be greater than 0, so will never enter the loop.

  2. Even if it gets into the loop, this is an infinite loop. Keep adding one to a variable would make it so the variable is always greater than 0, so will never exit the loop.

  3. MAJOR ONE: your variables are inside main, but you are using them outside of main!

    #include <stdio.h>
    Int main ()
    {
        /* define variable */
        int average_speed;
        int car_count;
        int car_speed;
    **};**
    
  4. (Not sure about this one) but you have an uppercase I in the word int in your method declaration, and uppercase WHILE, should be while.

benderman
  • 103
  • 7
-1

Code that is not within the main() function causes your errors.

//C code
//This program will calculate the average speed of passing cars using a WHILE-END loop       
//Developer: Jasmine Tucker
//Date: 7 Sept 2014
#include <stdio.h>

/* define variable */
//You Can define global variables or local variables. If you want to use them outside your
//function you Need to declare them globally as done here

    int average_speed;
    int car_count;
    int car_speed;

//This is where the Magic happens. If you execute your program it will jump to the main
//function and execute whatever is in there
int main ()
{

    /* set variable values */
    average_speed= 0;
    car_count=0;
    car_speed=0;

    /*WHILE-END loop */
    //The while Loop is not a function, you Need to put it in main() so it gets executed
    while(car_count > 0)
    {
        //You did use very strange signs here. The correct ones are These: ""
        printf("Enter the car’s speed %s:", car_count);
        scanf("%d", &car_speed);
        average_speed= car_speed / car_count; //You Forget a semicolon indeed ;-)
        car_count++;
    }
    printf("\n The average speed is %d miles per hour. \n", average_speed);
    return 0; 
} //semicolons at the end of a function are not used in C. They are used if you want to define a structure.

I strongly suggest that you start off with basic books / a wiki to C programming. Here is a good (at least it was for me) start into that: http://en.wikibooks.org/wiki/C_Programming/Intro_exercise

Haini
  • 922
  • 2
  • 12
  • 28
  • Thank you all so much for the responses! I am going to look them all over again lol I ended up having to just simplify my code and go another route just so I could get the assignment turned in on time. Haini thanks for the reference, I will definitely look into it. Are there any hard copy books you guys know of that could work as a good training resource for learning? – jastuck1 Sep 10 '14 at 20:40
  • Hello @jastuck1: I am glad that our answers could help you! Regarding hard cover books: I have never used one, but there is a pretty satisfying list of books [here on Stackoverflow](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). As these books tend to be pretty expensive I would recommend to have a look at the library of a technical University if you have one nearby. – Haini Sep 11 '14 at 13:41