whats the point? The code is not complex enough to make a difference...
Sturm
Veteran Poster
1,079 posts since Jan 2007
Reputation Points: 343
Solved Threads: 24
If this is your program:
#include <iostream>
void myFunction() {
// ...
}
int main() {
// ...
}
You need a prototype for the function:
myFunction.h
void myFunction();
Then you should put the function in its own .cpp file:
myFunction.cpp
#include "myFunction.h" // just to be safe
void myFunction() {
// ...
}
Finally, what's left is the main function:
main.cpp
#include <iostream>
#include "myFunction.h" // this is for our function prototype!
int main() {
// ...
}
That will work fine in some cases, but if you use the STL, for example, in the functions, you will need to add #includes to the .cpp file, or the .h file if necessary:
myFunction.cpp
// you might have to move this to the .h file
// if any STL objects are used in the function arguments
#include <iostream>
using namespace std;
void myFunction() {
// ...
}
[edit] A minor detail I forgot, you will need to make several more functions out of the code that is contained in main() before you can split it up.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>whats the point? The code is not complex enough to make a difference...
It could be a homework assignment. Most teachers won't force a student to learn splitting up code into several files by practicing on a 10,000 line project.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
if it was a homework assignment. Wouldn't he use functions....
Sturm
Veteran Poster
1,079 posts since Jan 2007
Reputation Points: 343
Solved Threads: 24
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
oh sorry missed the top bit.
Sturm
Veteran Poster
1,079 posts since Jan 2007
Reputation Points: 343
Solved Threads: 24