0
while(a<0)
{
    printf("\n O hai! How much change is owed?");
    scanf("%f",&a);
}

b=(int)roundf(float(a*100));

The above is a part of my code. Clang gives an error 'expected expression' even though I've followed the syntax clearly by referring to the manual(man round). However, when I compile with g++, it doesn't give me an error at all.

enter image description here

Please help!

1

Regarding the first error b=(int)roundf(float(a*100)); should be following, b=(int)roundf((float)(a*100)); However, typecasting to float is not needed as 'a' is already in float.

Roundf is from math.h library, you need to link that library while compiling. Try following:

clang -o greedy greedy.c -lm

'-lm' tells the linker to use math library. Try this two things and post what happens.

  • Yep, it's working now! Thanks! – Amrita Aug 18 '14 at 8:33
0

I believe you're trying to do one of these two things (or probably both).

  1. To cast the result of the expression a * 100 to float.

  2. To pass the expression a * 100 to roundf() assuming that it expects a float as an argument.

If you're trying to cast a value to float, then the correct syntax for doing this is

(float)value;

For example

int x = 10;
float f = (float)x; // cast the value in x to float

If rather you're passing the expression to roundf(), then you don't really need to include the datatype. Just the variable name/the value/the expression.

For example

float f = 10.5;
float r0 = roundf(f); // passing a variable
float r1 = roundf(9.3); // passing a value
float r2 = roundf(5.2 * 100); //passing an expression

That's a thing! Another thing is that you should be printing a newline and a white space before "O hai! How much change is owed?". And probably you should use GetFloat() from the CS50 library since it guarantees that you'll get a float as input from the user.

  • No I'm not trying to cast it. Just to be clear, 'a' is the float variable that demands the change. I declared float in 'roundf(float(a*100));' because it was listed in the manual. If I removed the float from there, clang gives me an error of 'undefined reference to roundf' . The weird thing is, when I run this with g++, it gives me no error, runs the program, and gives an output 18 for an input of 4.2. Is there something wrong with clang? because I ran update50 like an hour back. – Amrita Aug 18 '14 at 7:22
  • @Amrita the float between the parentheses in the prototype of roundf() is the type of data that should be passed to this function when called. You shouldn't include it when you call that function and pass data to it as I've show above. To fix the undefined reference to roundf() error, make sure you included the math.h header file atop your program by writing #include<math.h> and also make sure you linked the math library when compiling using the -lm option! Apparently, g++ is a C++ compiler. You should use either clang or gcc to compile C programs. – Kareem Aug 18 '14 at 14:18
  • Hey thanks! Yes, I'd included the header file, but didn't use -lm. And thanks for telling me about the compiler thing. It really cleared my confusion. :) – Amrita Aug 19 '14 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .