having more than one source file in the project

Sep 20, 2008 at 12:45pm
Hello again,
How to connect between two source files?
Lets say I have 2 .cpp files in one project:
1.The main program:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int a[10],c;

main()
{
      for(c=0;c<=9;c++)
      input(a[c],c);
      for(c=0;c<=9;c++)
      printf("\n%d",a[c]);
      scanf("%d",&c);
}

2.A function:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
void input(int& a,int c)
{
  
     
     printf("\nenter #%d variable:",c); 
     scanf("%d",&a);
     
}


Can someone please help me to get this working as if it was in one file like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int a[10],c;
void input(int& a,int c)
{
     
     
     printf("\nenter #%d variable:",c); 
     scanf("%d",&a);
     
}
main()
{
      for(c=0;c<=9;c++)
      input(a[c],c);
      for(c=0;c<=9;c++)
      printf("\n%d",a[c]);
      scanf("%d",&c);
}


Extreme-thanks in advance!
Last edited on Sep 20, 2008 at 12:52pm
Sep 20, 2008 at 1:06pm
Add a header file and include it in all your cpp source files

1
2
3
// file: header.h
#include <stdio.h>
void input(int& a,int c);


1
2
3
4
5
6
7
8
9
10
// file: cpp1.cpp
#include "header.h"
void input(int& a,int c)
{
  
     
     printf("\nenter #%d variable:",c); 
     scanf("%d",&a);
     
}


1
2
3
4
5
6
7
8
9
10
11
12
13
// file: cpp2.cpp
#include "header.h"
int a[10],c;
int main()
{
      for(c=0;c<=9;c++)
      input(a[c],c);
      for(c=0;c<=9;c++)
      printf("\n%d",a[c]);
      scanf("%d",&c);

      return 0;
}
Last edited on Sep 20, 2008 at 1:10pm
Sep 20, 2008 at 2:43pm
Thank you for answering my question, but when I try building it I get this error:

fatal error C1083: Cannot open include file: 'header.h': No such file or directory

what could be the reasons?

p.s I do have the 3 files in my project.

EDIT: alright, I just had some problems with the files' location, so its fixed and working now, thank you very much for the help sir!
Last edited on Sep 20, 2008 at 2:54pm
Topic archived. No new replies allowed.