2

I'm getting an error in this line:

int x = get_int();

clang says:

water.c:7:13: error: implicit declaration of function 'GetInt' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    int x = GetInt();

This line works fine in other programs, as copied from the lecture- what's up?

  • There is nothing visibly wrong with the code you have provided. Perhaps you can include your entire code? I used int x = get_int(); in a test program I made and there is no compilation error. I suggest refreshing, resaving, and re-compiling the file. If you are still getting errors, you might consider using GetInt() instead and see if that works. – SuperNovaCoder Jan 30 '17 at 14:15
  • I had the same problem. get_int() needs an argument in between the parentheses. I put (0) in and the program now runs as intended. I hope that helps. I bit into this one today. – lucidity.walking Jun 19 '19 at 16:13
1

implicit declaration of function 'GetInt' is invalid in C99 Though daunting, the compiler makes it very clear - your code implies that you want to use GetInt, but you instead use get_int(). I have found that, at times, the compiler and CS50 library can be very sensitive, so it is best to use the regular version, GetInt(). In the CS50 file, how GetInt is named/defined - get_int, Get_Int, GetInt, or any variation thereof - can affect how the function performs and how to call on that function.

I created a test program to show you how that works. Here is my test program:

// define function
bool function(int a, int b);

int main(void)
{
    // set variables
    int x = 0; 
    int y = 1;

    // check if function evaluates to true
    if (Function(y, x) == true)
    {
        printf("True!");
    }
}
// checks if a > b
 bool function(int a, int b)
{
    if (a > b)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

Since GetInt is a function, I chose to create a function called function that tests if int a is less than int b. You'll see that in the line if (Function(y, x) == true), I test to see if the function returns true. However, I use a different spelling - Function instead of function. This returns the same error that you get on your program:

something.c:13:9: error: implicit declaration of function 'Function' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
if (Function(y, x) == true)

When I change Function to function, the original spelling I define my function as, I can compile just fine without any errors. I hope that my example can explain why get_int might be considered invalid - and show you why you might get that error in the future.

int x = get_int() worked for me in another program I created, but I have seen from other answers that CS50 is case-sensitive. However, get_int should work, and if it doesn't, you might want to consider re-saving, re-compiling, and refreshing your file. However, if you want to go the "easy" route, you can change get_int() to GetInt().

If this answers your question, please click the green checkmark! Comment below if you still have issues/questions.

| improve this answer | |
  • Wow- more than I expected- thanks! I already solved the problem myself a few minutes after I posted (forgot the library). – Rick Feb 2 '17 at 3:01
  • @Rick No problem! Glad to hear you solved it. If you found the answer to your question, post it as an answer and accept it or accept one of the other answers - that way this doesn't stay in the unanswered section! – SuperNovaCoder Feb 2 '17 at 14:32
0

Did you #include <cs50.h>?

| improve this answer | |
  • Yeah- already figured that out- thanks. – Rick Jan 30 '17 at 7:26

You must log in to answer this question.

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