0

I am trying to complete the "Greedy" problem on CS50 and am running into some issues when attempting to compile my code so far.

Please see my code below:

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

int main(void)
{
    // GET THE CORRECT INPUT FROM THE USER

    float ChangeNeeded;

    do
    {
        printf ("So, how much change do you need? \n");
        ChangeNeeded = get_float();
    }
    while (ChangeNeeded < 0);

    // CONVERT THE VALUE INTO CENTS
    int RoundedChange = round(ChangeNeeded*100);
    printf ("so you need %i cents\n", RoundedChange);

    // DISTRIBUTE CHANGE AND COUNT COINS
    int CoinCount = 0;
    int Quarter = 25;

    do
    {
        RoundedChange / Quarter;
        ++CoinCount;
    }
    while (RoundedChange >= Quarter);

    printf ("The least amount of coins i can give is %i\n", CoinCount);
}

The error messages are as below:

~/workspace/workspace/pset1/ $ clang greedy.c
greedy.c:37:47: warning: expression result unused [-Wunused-value]

                                RoundedChange / Quarter;

                                ~~~~~~~~~~~~~ ^ ~~~~~~~
1 warning generated.

/tmp/greedy-cf0462.o: In function `main':
greedy.c:(.text+0x2a): undefined reference to `get_float'
greedy.c:(.text+0x61): undefined reference to `round'
clang: error: linker command failed with exit code 1 (use -v to see
 invocation)

At first I thought there was an issue linking the "Math.h" librqry but I am not sure this is the case as when I also try to compile using the following command:

~/workspace/workspace/pset1/ $ clang greedy.c -lm

I get the following errors:

greedy.c:37:47: warning: expression result unused [-Wunused-value]

                                RoundedChange / Quarter;

                                ~~~~~~~~~~~~~ ^ ~~~~~~~

1 warning generated.

/tmp/greedy-a1b3a8.o: In function `main':
greedy.c:(.text+0x2a): undefined reference to `get_float'
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)

if Anyone could help me see my mistake I would be very grateful.

Thanks in advance.

5

You have multiple issues.

Firstly, your linker errors undefined reference to ... means the linker can't find the functions you are using. In order to tell clang to link with the cs50 library for get_float you need the -lcs50 option. In order to use the math library functions (such as round) you need the -lm option.

Instead of calling clang -lcs50 -lm each time... you should be using make. make is setup by the course administrators to link the appropriate libraries, and set other clang options.

Use make greedy to avoid these problems. The more complex a program is, the more important it is to use make instead of a long clang command each time you compile.

Secondly, the warning warning: expression result unused [-Wunused-value] is telling you something very important about your code.

RoundedChange / Quarter;

This statement takes the value stored in RoundedChange and divides it by the value stored in Quarter. And nothing else. The result is not stored anywhere - which is what the compiler means by expression result unused.

It's easier to see if you use constant values instead of variables:

x = 42 / 7;

is different than

42 / 7;

You need to store the result using one of these two methods:

RoundedChange = RoundedChange / Quarter;

or

RoundedChange /= Quarter;

Lastly is a logic problem. If I have 100 cents and take a quarter away from that... do I divide 100 and 25 to get the number of cents remaining? or should I subtract a quarter? Good luck!

| improve this answer | |

You must log in to answer this question.

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