cs50 / libcs50

This is CS50's Library for C.
https://cs50.readthedocs.io/libraries/cs50/c/
GNU General Public License v3.0
1.79k stars 861 forks source link

get_string function prints out special characters instead of the given prompt #240

Closed Senya247 closed 3 years ago

Senya247 commented 3 years ago

Here's my simple code-

image

I compile it with clang -lcs50 test2.c -o hello it generates no errors, but generates these warnings-

test2.c:6:16: warning: implicit declaration of function 'get_string' is invalid in C99 [-Wimplicit-function-declaration] string s = get_string("Input: "); ^ test2.c:6:12: warning: incompatible integer to pointer conversion initializing 'string' (aka 'char *') with an expression of type 'int' [-Wint-conversion] string s = get_string("Input: "); ^ ~~~~~~~~~~~~~~~~~~~~~ 2 warnings generated.

then when i execute it with ./hello, i get this weird prompt image this peice of code works fine on the online cs50 ide, so something must be wrong on my computer. Here's my computer's info- image I'd installed the library with $ curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash $ sudo apt-get install libcs50 How do I fix this?

nuoxoxo commented 3 years ago

You have compiled the code with -o. As a result, hello will be an object.

When you print an object out, it displays the machine language, hence the "special characters".

I think you can try again and compile it using make since you have the libcs50.

dmalan commented 3 years ago

@Nightmare247, sure you didn't take any other steps (e.g., manually editing cs50.h once installed)? I just did

$ docker run -it ubuntu:20.10
$ apt-get update
$ apt-get install -y clang curl sudo vim
$ curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash
$ sudo apt-get install libcs50
$ vim test2.c # after which I added your code and saved
$ clang -lcs50 test2.c -o hello

and that indeed compiled and executed okay.

And what do you see if you execute cat /usr/include/cs50.h?

Senya247 commented 3 years ago

@dmalan , nope I never edited the file, also, all other functions like get_int and get_float work fine and here's what I see when i do cat /usr/include/cs50.h `/**

ifndef CS50_H

define CS50_H

include

include

include

include

include

/**

/**

/**

/**

/**

/**

/**

/**

endif

`

kzidane commented 3 years ago

@Nightmare247 if you navigate to the folder where your source code file is and run the following commands, what is the output?

echo $0
apt-cache policy libcs50
cat ./test2.c
clang --version
clang ./test2.c -lcs50 -ohello
./hello
jms41276 commented 3 years ago

Those warnings are what is seen when is not included. They can be promoted to errors with clang -lcs50 -test2.c -o hello -Werror. The problem recreates on my machine with that header include removed, using the original compile command. Using gdb shows that name does get set to what is typed in. Or if aborted, that get_string from libcs50.so.10 was called. Could there be a path problem, some older cs50.h is found first?

Senya247 commented 3 years ago

@kzidane here's the output of all the commands image

It still outputs weird characters.

Senya247 commented 3 years ago

@jms41276 I thought at first that some older version was being used, so I reinstalled libcs50 with sudo apt remove libcs50 && sudo apt install libcs50, but that didn't change anything, also, here;s the output of locate cs50.h if it helps in any way

image

kzidane commented 3 years ago

Sounds like you have multiple versions of cs50.h? If you installed the library using apt (or apt-get), you should only have /usr/include/cs50.h. Did you install the one at /usr/local/include/cs50.h manually? If so, can you try removing it or moving it somewhere else temporarily and try again?

Senya247 commented 3 years ago

@kzidane Thank you so much! I moved /usr/local/include/cs50.h somewhere else and now it finally works!