Help explaining something

Mar 30, 2016 at 12:16am
closed account (owCkSL3A)
Could some one explain this line of my assignment further?

"The function “find_e” must be contained in a separate file from the file containing the “main” function. "

Do i need 2 cpp files ?

thanks,
Last edited on Mar 30, 2016 at 12:19am
Mar 30, 2016 at 12:33am
It does sound like the assignment wants you to add more than one file. Have you learned about adding more files? What IDE are you using? To use multiple files you will need to use something called a header file. Depending on the IDE you are using there will be a different way to add a header file. You then use a #include statement in your main.cpp. Here is an Example:

Other_Functions.h
1
2
3
4
5
6
#ifndef Other_Functions_h
#define Other_Functions_h

void printX(int x); // Just like a function declaration in your main.cpp file

#endif 


Other_Functions.cpp
1
2
3
4
5
6
7
8
#include "Other_Functions.h"
#include <iostream>
// Now you just write the function like any other function
void printX(int x){
std::cout << x << std::endl;
return;
}
}


main.cpp
1
2
3
4
5
6
7
#include "Other_Functions.h" // Then you include the .h file in your main.cpp

int main(){
int x = 20;
printX(x); // And now you can use it just like any other function
return 0;
}
Last edited on Mar 30, 2016 at 12:57am
Topic archived. No new replies allowed.