-2

I am trying to make this simple code for practice in C. It asks the user to give a positive number, checks if it's positive or not, then returns just positive numbers.

I am getting this error:

positive.c:28:7: warning: implicit declaration of function 'GetInt' is invalid
  in C99 [-Wimplicit-function-declaration]
            n = GetInt();

I would have thought this meant I didn't declare one of my functions or that I didn't call in some library. To the best of my knowledge, I did all of this. Here is my code:

#include <stdio.h>

int GetPositiveInt(void);

int main(void)
{
    int n = GetPositiveInt();
    printf("Thanks for the %i\n", n);
}


/*This all gets called into the above bit*/
int GetPositiveInt(void)
{
    int n; /*declare the variable*/
    do
    {
        printf("Give me a positive integer: ");
        n = GetInt();
    }
    while (n <= 0);
    return n; /*return variable to above*/
}

Does anyone have any ideas on why this is giving me an error?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Berzerkeley
  • 47
  • 3
  • 9
  • 2
    what's getint and where did you get it from? – zubergu Oct 10 '13 at 06:16
  • this has to be covered somewhere, what research have you done? – Grady Player Oct 10 '13 at 06:20
  • possible duplicate of [What is the difference between a definition and a declaration?](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – Grady Player Oct 10 '13 at 06:21
  • Grady Player: I was following a course from Harvard, CS50, which actually uses it's own library that has pre-made functions, one of them being GetString. In my own ignorance, I had no idea that GetString was not a normal function of C, so I assumed it should work. – Berzerkeley Oct 14 '13 at 04:29

4 Answers4

2

That's because this function, GetInt does not exist or you forgot to include the correct header.

You can replace the call to the function GetInt by:

scanf("%d", &n);
Maxime Chéramy
  • 14,912
  • 7
  • 43
  • 67
1

You haven't declared GetInt(). Compiler don't know what it returns and what arguments it receives. Implicit declarations was forbidden in C99 (was valid before - would only produce warning if you'll enable C89).

Of course if you'll have only declaration and no implementation - you'll get an error on linking phase.

keltar
  • 15,618
  • 2
  • 31
  • 40
1

There's a library created for the CS50 edX class you need to include at the top of your .c file. Otherwise the compiler doesn't know about the GetInt() function.

#include <cs50.h>
qallar
  • 56
  • 4
0

Worked for me: You may use get_int(); in the CS50 IDE and compile it using make or simply put -lcs50 at the end of clang.