0

So my program requires me to make a function in one file, and call it into another.

I have one file called convertdays.cpp like this:

#include <iostream>

int convertdays(int, int, int); 

int convertdays(int month, int day, int year)
{
int date; 
date = year * 1000 + month * 100 + day; 
return date;
}

Then I have another file where my int main() stuff is, like this:

#include <iostream>
using namespace std; 

int main()
{
int day, month, year; 

cout << "Please enter the month: " << endl; 
cin >> month; 

cout << "Please enter the day: " << endl;
cin >> day; 

cout << "Please enter a year: " << endl; 
cin >> year; 

convertdays(month, day, year); //convertdays stays in red though.

//Still need to add a couple of more things but not necessary right now.

system("Pause"); 
return 0; 
}

How do I make this work where I can keep all of my functions in another file and call them in when I need them?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Burori
  • 1
  • 3

1 Answers1

2

Make a file called "convertdays.h", containing the function declaration:

int convertdays(int, int, int); 

This is called a header file.

Then at the top of main.cpp:

#include "convertdays.h"

(It's a good idea to put the same thing at the top of convertdays.cpp, though not strictly necessary.)

Then when you build the executable, link main.o and convertdays.o.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • I made another file called convertdays.h and put the declaration in there. I do not understand what "Then when you build the executable, link main.o and convertdays.o." means. I'm sorry I'm new so I'm not sure what to do there D: – Burori Feb 08 '16 at 00:23
  • 1
    @Burori That's explained in depth in the duplicate I used to clos your question – πάντα ῥεῖ Feb 08 '16 at 00:25