0

Fro the files int.c and string.c, I copied the code from the lecture exactly, and was receiving errors, then modified the code according to suggestions online by adding #include <cs50.h>and I'm still receiving multiple errors when I try and compile it using both clang and make

For int.c my code is:

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

int main (void)
{
    int i = get_int("Integer: ");
    printf("hello, %i\n", i);
}

And the errors I get:

~/workspace/ $ make int
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    int.c  -lcrypt -lcs50 -lm -o int
int.c:5:13: error: implicit declaration of function 'get_int' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    int i = get_int("Integer: ");
            ^
int.c:6:21: error: more '%' conversions than data arguments [-Werror,-Wformat]
    printf("hello, %i\n");
                   ~^
int.c:5:9: error: unused variable 'i' [-Werror,-Wunused-variable]
    int i = get_int("Integer: ");
        ^
3 errors generated.
make: *** [int] Error 1

Similarly for the file string.c my code is:

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

int main(void)
{
    string s = get_string("Name: ");
    printf("hello, %s\n", s);
}

And the error I get:

~/workspace/ $ clang string.c
string.c:5:5: error: use of undeclared identifier 'string'; did you mean 'stdin'?
    string s = get_string("Name: ");
    ^~~~~~
    stdin
/usr/include/stdio.h:168:25: note: 'stdin' declared here
extern struct _IO_FILE *stdin;          /* Standard input stream.  */
                        ^
string.c:5:11: error: expected ';' after expression
    string s = get_string("Name: ");
          ^
          ;
string.c:5:12: error: use of undeclared identifier 's'
    string s = get_string("Name: ");
           ^
string.c:5:16: warning: implicit declaration of function 'get_string' is invalid in C99 [-Wimplicit-function-declaration]
    string s = get_string("Name: ");
               ^
string.c:6:27: error: use of undeclared identifier 's'
    printf("hello, %s\n", s);
                          ^
string.c:5:5: warning: expression result unused [-Wunused-value]
    string s = get_string("Name: ");
    ^~~~~~
2 warnings and 4 errors generated.

Any help will be greatly appreciated! (:

1

Your line numbers in code and in the error messages don't match, are off by one, so I'd guess you forgot to save, or compiled something else (another file, maybe of the same name in a different directory).

A successful make run might print the command used to call the compiler, but the compiler itself will probably not print anything, just create a int file you can execute.

| improve this answer | |
  • Thank you so much for your response. You are right - the line numbers in my code and error messages don't seem to match for anything that I do, any ideas why that might be, please? – Xia Liu Jul 13 '18 at 22:34
  • My guess would be you copy&pasted the error messages from the files before adding the #include <cs50.h> line. This could be due to forgetting to save before calling make, having multiple versions of the code in different files (maybe same name but in different directories), not running make again after saving the changes, or accidentally copying output from a previous make run. – Blauelf Jul 16 '18 at 8:14
  • I've probably done all of these in one way or another. – Blauelf Jul 16 '18 at 8:17
1

Solved! I ran "update50" in the terminal twice, and after the second time a prompt came up that told me I'm currently running "pip 9.0.1" and there is an update available for "pip 10.0.1", and told me what command to type for the update.

After I entered the command, I waited about 20 seconds, made sure my code was correct and typed make int again - this time, no errors!

| improve this answer | |
  • Wow. If that fixed it, this means you probably had a really old copy of cs50 library, one still using GetString instead of get_string. Just that even the old version should have included a typedef for string. And pip is probably unrelated, as this is for Python. update50 would be the relevant part (I just thought there is a big message on the top whenever one can update, but that might not always work) – Blauelf Jul 12 '18 at 15:32
0

I am glad you shared your solution.

I understand that the CS50 implementation for GetString() and get_string() were aimed at simplifying the lesson for students -> BUT it is a great lesson of the unintended consequences, confusion, and complexity that can be introduced. I am curious why not introduce scanf or cin in the first lessons VERSUS a new function using a custom header file.

| improve this answer | |
  • I cannot agree, first of all cin, C does not belong, as for scanf (), it is not desirable to use it and I am sick of seeing the problems it creates, we can discuss the pedagogical value of using the cs50.h library , but I would support to make this library a standard, in other languages much more "dark" functions are used for the programmer and nobody says anything. – MARS Jun 5 at 18:48

You must log in to answer this question.

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