Thread: Need help with creating a separate CPP file for prototype funvtion

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    Need help with creating a separate CPP file for prototype funvtion

    I need to create a separate cpp file to pull a function from, my book and power point slide don't go into much detail on this. Please help.
    "Include at least one function in a separate .cpp file."

    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<limits>
    usingnamespace std;               
    
    
    
    int main()
    {
    char again;// assigned variable for run again loop
    do
     {
       int n;//n=number of computers
       int conn;// conn= total of connections
    
       cout <<"\t*********************************;
       cout <<******************************\n";
       cout <<"\t*This program will display a list;
       cout << of how many possible network*\n";
       cout <<"\t*connections you could have;
       cout << given any number of computers.*\n";
       cout <<"\t*********************************;
       cout <<******************************\n";
       cout <<"\n";
       cout <<"Enter the maximum number of computers\n";
       cout <<for which you would like to;
       cout <<calculate total connections:";
       cin >> n;
    
    
             //validate the input
        while(!cin || n <2)
    {
            //Explain the invalid input.
    
            cin.clear();
            cin.ignore(numeric_limits <streamsize>::max(),'\n');
            cout <<"You must enter a number range from 2-10\n";
            cout <<"Please enter your selection again: \n";
            cin >> n;
    }   
        //Block that will open file, write to file, and display to the console
        cout <<"\n";
        ofstream myFile;
        myFile.open("totalComputers.txt");
        cout <<"Number of Computers\tTotal Connections\n";
        myFile <<"Number of Computers\tTotal Connections\n";
        cout <<"////////////////////////////////////////\n";
        myFile <<"////////////////////////////////////////\n";
    
    
        for(int count=2; count <= n; count++)
            {
    
                conn = count *(count -1)/2;
                cout << setw(10)<< count << setw(24)<< conn << endl;
                myFile << setw(10)<< count << setw(24)<< conn << endl;
    
            }
              //This was implemented to close the file, 
              //and notify user that data write is complete.
                myFile.close();
                cout <<"\n";
                cout <<"The data file has finished writing.\n";
    
    
    
    
         //This will allow the user to run the program again or exit the program
           cout <<"\n";
           cout <<"Would you like to run the program again? y or n\n";
           cin >> again;
    }while(again =='y');
           cout <<"Have a great day, please press any key to exit.\n";
    
    return0;
    
    }
    


  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It depends on what tools you're using. If using any decent IDE, you should be able to create a project and inside that include all the source file you need to compile into a single executable. If not, and you're just using the command line, it's as easy as passing all the source files(*.cpp) at once.

    Also, there are many errors in the code that you posted, you may want to check them out.
    Devoted my life to programming...

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with using a Function from a separate file.
    By RelentlessWiz in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2011, 03:33 PM
  2. #define from a separate file
    By Syekiya in forum C Programming
    Replies: 6
    Last Post: 09-19-2010, 03:33 AM
  3. Call a funvtion from a variable
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-18-2009, 07:22 AM
  4. Functions in a separate file
    By Fox101 in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 02:53 PM
  5. WinProc in separate file
    By ChrisWard1000 in forum Windows Programming
    Replies: 2
    Last Post: 09-10-2004, 07:27 AM