implicit declaration of function getchar(...)?

This is a discussion on implicit declaration of function getchar(...)? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <conio.h> #include <iostream.h> int prime(int n) { int i; if (n % 2 == 0) return (n==2); if ...

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Question implicit declaration of function getchar(...)?

    Code:
    #include <conio.h>
    #include <iostream.h>
    int prime(int n)
        {
        int i;
        if (n % 2 == 0)
        return (n==2);
        if (n % 3 == 0)
        return (n==3);
        if (n % 5 == 0)
        return (n==5);
        for (i=7; i*i <= n; i+=2)
        if (n % i == 0)
        return 0;
        return 1;
    }
    
    void main(void)
        {
        int i, n;
        n = 1000;
        clrscr();
        cout << "ENTER THE NUMBER :-";
        cin >> i;
        if (prime(i))
        cout <<"\n THE NUMBER IS PRIME.";
        else
        cout << "\nTHE NUMBER IS NOT PRIME.";
        getch();
    }
    it gives me the error 'implicit declaration of function getchar(...)'what does it mean?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Your prime() function should return bool, not int. Use the standard headers <XXXX>, not <XXXX.h>. You really don't need getchar() here.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36
    Code:
    #include <conio.h>
    #include <iostream>
    #include<cstdlib>
    bool prime(int n)
        {
        int i;
        if (n % 2 == 0)
        return (n==2);
        if (n % 3 == 0)
        return (n==3);
        if (n % 5 == 0)
        return (n==5);
        for (i=7; i*i <= n; i+=2)
        if (n % i == 0)
        return 0;
        return 1;
    }
    
    void main()
        {
        int i, n;
        n = 1000;
        cout << "ENTER THE NUMBER :-";
        cin >> i;
        if (prime(i))
        cout <<"\n THE NUMBER IS PRIME.";
        else
        cout << "\nTHE NUMBER IS NOT PRIME.";
        system("PAUSE");
        return 0;
    }
    Thanku it works now. But y? it gives me the error with clrscr(); and getch(); ?

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Those two functions are non-standard, so I don't bother using them.

    There are still a few problems with your code. Most notably, change main()'s return type to int before you get flamed. Also, some better indenting would help.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    You're using Dev-C++, right?
    Basically, Dev-C++ uses the MinGW port of GCC as its default compiler, and GCC doesnt support conio.h (well), since that is more for Borland's compilers.
    However, since you dont even need conio.h in your program, you can just take it out, e.g.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using std::cout;
    using std::cin;
    
    bool prime(int n)
    {
    	if (n % 2 == 0)
    		return (n == 2);
    	if (n % 3 == 0)
    		return (n == 3);
    	if (n % 5 == 0)
    		return (n == 5);
    	for (i = 7; i * i <= n; i += 2)
    		if (n % i == 0)
    			return 0;
    	return 1;
    }
    
    int main()
    {
    	int i;
    	cout << "ENTER THE NUMBER :-";
    	cin >> i;
    	if (prime(i))
    		cout <<"\n THE NUMBER IS PRIME.";
    	else
    		cout << "\nTHE NUMBER IS NOT PRIME.";
    	system("PAUSE");
    	return 0;
    }

  6. #6
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36
    thanku

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21