2

After I update cs50 IDE, the get_int() comes to error and I can't even compile the simplest program.

#include <stdio.h>
#include <cs50.h>
int main()
{
    int s=get_int();
    printf("%i",s);
}

error message:

clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    test2.c  -lcrypt -lcs50 -lm -o test2
test2.c:8:11: error: missing field 'prompt' initializer [-Werror,-Wmissing-field-initializers]
    int s=get_int();
          ^
/usr/include/cs50.h:117:62: note: expanded from macro 'get_int'
#define get_int(...) get_int(&(struct prompt) {0, __VA_ARGS__})
                                                             ^
1 error generated.

Can someone help me out?

1
0

The definition of get_int() in cs50.h has changed. Try man get_int in a terminal and the following will show:

NAME

get_int - prompts user for a line of text from stdin and returns the equivalent int

SYNOPSIS

   #include <cs50.h>

   int get_int(string prompt);

See that there is now a prompt parameter in get_int(). If you want nothing to show as a prompt, just pass an empty string as an argument.

Example:

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

int main(void)
{
    // with prompt
    int x = get_int("What is x's value? ");
    printf("x was %d\n", x);

    // without prompt
    x = get_int("");
    printf("x was %d\n", x);
}

with output:

What is x's value? 20
x was 20
30
x was 30

Although in the sourcecode in Github they mention that the prompt should be optional, it doesn't appear to be so. They might solve it in a future commit.

5
  • Thank you for your constructive input to this issue, I am now able to proceed with compiling. Are you able to tell me why in the compiler warning it takes reference to #define, is it because the makro has now been made like #define, i.e.Formal Makro-definition-parameters on the right?. – Der Traumwanderer Jul 15 '17 at 9:40
  • What it shows is on this line. I think it's a kind of default constructor in case the user doesn't provide a prompt, but obviously something went wrong. I'm not sure though. – ChrisG Jul 15 '17 at 9:44
  • Upon further research, I found this header file cstdarg which allows functions to have a list of parameters, whose number and types are not known to the called function. And you can also do the same for macros, called Variadic Macros (as they do) as shown here. You could dive into it more on your own if you want. Till then, Happy coding! :) – ChrisG Jul 15 '17 at 9:49
  • 1
    The argument is optional (as a matter of fact, it won't work with the current check50 being used in the 2017 edX course) and the bug that was requiring it to be used has been fixed in the latest update50. – curiouskiwi Jul 16 '17 at 0:00
  • Last time I run update50 was about that time yesterday, and I couldn't compile the program when using get_int() without the argument. After another update50 I did right now, I can compile. So you are right, but at the time of my answer the new update50 hadn't reached me yet, so I guess the answer is also right, in it's time frame. I can delete it if it will just confuse though. – ChrisG Jul 16 '17 at 12:23

You must log in to answer this question.

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