Confusion using More than 1 file

This is a discussion on Confusion using More than 1 file within the C++ Programming forums, part of the General Programming Boards category; Main file named main.ccp Code: #include <iostream> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> using ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Confusion using More than 1 file

    Main file named main.ccp
    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char choice;
        printf("\n");
        Sleep(100);    
        printf("\n");  
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\t\t\t       By Me\n");
        Sleep(100);
        printf("\n");
        Sleep(100);    
        putchar('\n');   
        Sleep(100);
        printf("\t\t\t        [N]ew Game\n");
        printf("\t\t\t        [Q]uit\n");
        Sleep(100);
        printf("\n");
        Sleep(100);
        printf("\n");
        choice=toupper(getch());
        for(;;)
        {
           if(choice=='N')
           {
               game();
               break;       
           }
           if(choice=='Q')
           {
              break;
           }    
        }
        return 0;
    }
    Begining of game file named game.ccp
    Code:
    //----------------------------------------Include Files-----------------------//
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    //----------------------------------------------------------------------------//
    //-----------------------------------//--------------------------------------//
    int game()
    {
    //----------------------------------------------------------------------------//
    //----------------------------------Random Variables--------------------------//
    int gold = 0;                  //Gold.
    char choice;                   //Choices in game A,F,R,E.
    int inumb;                     //Input number in gmae
    int GetRand(int min, int max); //Random number gen.
    int x;              //Counter for size of sentence.  
    int size;           //Size of sentence.
    
    //Town 1 variable//
    int beeninbar=0;
    int beenintemple=0;
    int population=263;
    
    //Alments//
    int disease=0;
    int weak=0;
    These 2 files are connected by the dev project file but for some reason it says that game is undeclared when i go to compile this file and im not sure why

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because it is. You need a .h file for your game.cpp file that contains the prototypes of your functions included in the file.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Errrm well ive seen a game from this site that used the method im trying to use and i can still compile the file and i dont have header files for the peices and wat do u mean?

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Nathan the noob View Post
    Errrm well ive seen a game from this site that used the method im trying to use and i can still compile the file and i dont have header files for the peices and wat do u mean?
    Defining functions somewhere in the code and expecting the compiler to know where they are is no good.

    Your code is parsed from top to bottom. This means that all functions defined after main that are used in main will not be 'known' by the compiler until it finished parsing main. This is solved by prototyping them at the beginning of the code.

    The prototypes were probably somewhere else, if they weren't in some included header file.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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