David Hancock David Hancock - 2 months ago 21
C Question

Can't define a variable

New to C, writing this simple program. However it throws an 'unused error', when i try to declare any type of variable and assign it a value. Below I use 'float' as the variable type and try and assign it to 1.5.

#include <stdio.h>
#include <cs50.h>

int main(void)

{
printf("How long is your shower?\n");
int time = GetInt();

float flow = 1.5;

}


Throw this error:

~/workspace/pset1/ $ make water
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow water.c -lcs50 -lm -o water
water.c:10:11: error: unused variable 'flow' [-Werror,-Wunused-variable]
float flow = 1.5;
^
1 error generated.
make: *** [water] Error 1

Answer

It's actually a warning and not an error but because of the -Werror flag you see it as an error.

Long story short, if you use the variable it won't return the error anymore.

#include <stdio.h>
#include <cs50.h>

int main(void)

{
    printf("How long is your shower?\n");
    int time = GetInt();

    float flow = 1.5;
    printf("Flow: %.2f, time: %d", flow, time);
}