-2

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."

I understand this is the format, but i'm lost how to do it. For each function argument, the prototype must include the data type of each parameter inside its parentheses the header must include a declaration for each parameter in its ()

void evenOrOdd(int);  //prototype
void evenOrOdd(int num) //header
evenOrOdd(val);       //call

#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace 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";

return 0;

}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

0

I'm not quite sure if I understood your question correctly. I'm assuming all you have to do is having evenOrOdd implemented in a different cpp-file. (Also I'm returning an int since I don't understand what the function would do if it returned void) Consider this an (almost) minimal example of how to have multiple cpp files.

This example is constituted of three files: foo.h, foo.cpp and main.cpp

foo.h:

/*
 * This file contains the declaration. Prototype
 */

int evenOrOdd(int); //prototype

foo.cpp (This is the "Include at least one function in a separate .cpp file."-file):

/*  
 *  This file contains the implementation
 */
#include "foo.h"

//returns 0 if even and 1 if odd
int evenOrOdd(int n){
    return n%2;
}

main.cpp:

#include "foo.h"
#include <iostream>

int main(){
    int val = 5;
    std::cout << evenOrOdd(val) << "\n";
    return 0;
}

Then compile as follows: First run

g++ -c foo.cpp

This produces the object-file foo.o. Then you compile main an link foo.o into it with:

g++ main.cpp foo.o -o main

and run with

./main

which should output 1 since val is 5 and 5%2 is 1.

I'm not sure this is exactly what you needed, but I hope it gives you an idea of how to possibly solve your problem.

Note 1: I did not follow best practices (like for instance header guards) in this example in order to keep it more or less minimal.

Note 2: I implemented evenOrOdd in a cpp-file because I understood your task asks you to do so, however you could have implemented it in the foo.h directly and then compiling would have just been:

g++ main.cpp -o main

Some ref-material:

Community
  • 1
  • 1
dingalapadum
  • 2,077
  • 2
  • 21
  • 31