#include, loading, and c++
#include, loading, and c++
- Subject: #include, loading, and c++
- From: <email@hidden>
- Date: Fri, 31 Mar 2006 10:38:42 -0500
Hi,
According to the xcode help
2.2 Include Operation
The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file. The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the #include directive. For example, if you have a header file header.h as follows,
char *test (void);
and a main program called program.c that uses the header file, like this,
int x;
#include "header.h"
int
main (void)
{
puts (test ());
}
the compiler will see the same token stream as it would if program.c read
int x;
char *test (void);
int
main (void)
{
puts (test ());
}
The problem is when I put it to use:
The following works:
************ main ***********
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
char RandomChar (int i)
{
int x;
char Let;
x = rand() % 26;
/*if (0 == i % 3)
Let = 'A';
else*/
Let = 'A' + x;
return Let;
};
void CreateData(int max)
{
char L;
ofstream fout;
fout.open("causal_data.txt", ios::out);
for (int i = 0; i < max; i++)
{
L = RandomChar(i);
fout << L;
}
fout.close();
}
int main (int argc, char * const argv[]) {
int max;
cout << "Enter number of iterations to use" << endl;
cin >> max;
cout << endl;
CreateData (max);
return 0;
}
but if I remove the random function and place it in a separate file as so, it does not
************ main ***********
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
#include "randomChar.cpp"
int main (int argc, char * const argv[]) {
int max;
cout << "Enter number of iterations to use" << endl;
cin >> max;
cout << endl;
CreateData (max);
return 0;
}
***************** randomChar.cpp *************
char RandomChar (int i)
{
int x;
char Let;
x = rand() % 26;
/*if (0 == i % 3)
Let = 'A';
else*/
Let = 'A' + x;
return Let;
};
void CreateData(int max)
{
char L;
ofstream fout;
fout.open("causal_data.txt", ios::out);
for (int i = 0; i < max; i++)
{
L = RandomChar(i);
fout << L;
}
fout.close();
}
Will someone explain this to me
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden