hi all,
i am done with my project for lexeme but i only worked on cpp, i wonder that can i make my header file based on my cpp file?
thank you
this is my code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{	
  char a;
  string str = "";

  ifstream file ("sample.txt",ios::in);
  if(!file)
		{
		cout<<"Unable to open the file!!!" <<endl;
		exit(1);
		}
  file >> a;
 
  while (file)
	{ 
		if (a == '/' ||a == '*' ||a == '+' ||a == '-' ||a == ';' ||a == '^'||a == '='||a == '('||a == ')')
		{cout << a << " symbol" << endl ;
		file >> a;}

		else {str = a;file >> a;
			while( a >= '0'  && a <= '9' || a == '.')
		{				
			str.append(1,a);		
			file >> a;
		}
		cout << str << " number" <<endl ;
			str = "";
		}	
	}

	file.close();

  return 0;
}

What exactly do you mean by 'make my header file based on cpp'.
Do you want to create a header file that you can include that contains a function for what your doing?
if so you would want to make a header file that contained a prototype of the function, the function (possibly in a seperate C++ file, and the main program. EG:

// TheHeader.h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void ReadFile();
//ReadFile.cpp
void ReadFile()
{
 char a;
  string str = "";
 
  ifstream file ("sample.txt",ios::in);
  if(!file)
		{
		cout<<"Unable to open the file!!!" <<endl;
		exit(1);
		}
  file >> a;
 
  while (file)
	{ 
		if (a == '/' ||a == '*' ||a == '+' ||a == '-' ||a == ';' ||a == '^'||a == '='||a == '('||a == ')')
		{cout << a << " symbol" << endl ;
		file >> a;}
 
		else {str = a;file >> a;
			while( a >= '0'  && a <= '9' || a == '.')
		{				
			str.append(1,a);		
			file >> a;
		}
		cout << str << " number" <<endl ;
			str = "";
		}	
	}
 
	file.close();
}
// MainProgram.cpp
#include "TheHeader.h"
int main ()
{
         ReadFile();
         return 0;
}

DarthPJB;987403]
Do you want to create a header file that you can include that contains a function for what your doing?
if so you would want to make a header file that contained a prototype of the function, the function (possibly in a seperate C++ file, and the main program.

Thank you very much for ur help, actually i wonder how can i make header file with private and public data?
as i know, private data should be string str... but that is the only thing i know, how can i make public and private data for header file.?

> actually i wonder how can i make header file with private and public data?
Yes, they're called classes.
Which is pretty much the root concept of C++ programming.
Time to read the book again.

Yes, they're called classes.
Which is pretty much the root concept of C++ programming.
Time to read the book again.

yes, that is what i got trouble right now, i read through out book but i still confuse how to use it, that is why i ask help.... if you can help me please, thank you

yes, that is what i got trouble right now, i read through out book but i still confuse how to use it, that is why i ask help.... if you can help me please, thank you

I'll try to explain as simplistically as I can.
Please don't be insulted if I'm wrong but I'm guessing english is not your first language, I'm also guessing your C++ book is in english and the jargon is confusing the hell out of you.

The basics:
A class is defined by using the 'class' keyword and then the name of your class, then a block containing data types and functions. For example:

//this is the header.h
class ExampleClass
{
       int ADataType;
       void ASampleFunction();
};

The above is what goes into your header file (.h), however as you can tell the function is not defined in the header (there is no code for ASampleFunction). We define this later:

//now in a cpp file
#include "header.h"
void ExampleClass::ASampleFunction()
{
//We will do something here
}

Note that you must put the class name before the function name (ReturnType ClassName :: FunctionName ())

Now for Public and Private.

when defining the class (in the header) we can state some data is public and other data is private with the keywords. For example:

//this is the header.h
class ExampleClass
{
private:
       int ADataType;
public:
       void ASampleFunction();
};

This means that 'ADataType' CANNOT be used by anything outside of the class. For example:

ExampleClass TheObject; //This defines a new 'ExampleClass' named 'TheObject'
TheObject.ADataType=1; //ERROR CANNOT ACCESS PRIVATE MEMBER!

TheObject.ASampleFunction(); //This is OK, ASampleFunction() is public!

In short ONLY public members can be used from outside the class (your program)
However, private members CAN be used inside the class...

void ExampleClass::ASampleFunction()
{
ADataType = 1;  //This is OK, We are In ExampleClass, So Private Can be used!
}

To make this perfectly clear I'll change my code from before.

// TheHeader.h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class FileReader
{
private:
    bool ReadBefore;
public:
    FileReader();
    void ReadFile();
}

ReadFile:

//ReadFile.cpp
#include "TheHeader.h"
FileReader::ReadFile()
{
    ReadBefore=false;
}
void FileReader::ReadFile()
{
    if(ReadBefore)return;
 char a;
  string str = "";

  ifstream file ("sample.txt",ios::in);
  if(!file)
        {
        cout<<"Unable to open the file!!!" <<endl;
        exit(1);
        }
  file >> a;

  while (file)
    { 
        if (a == '/' ||a == '*' ||a == '+' ||a == '-' ||a == ';' ||a == '^'||a == '='||a == '('||a == ')')
        {cout << a << " symbol" << endl ;
        file >> a;}

        else {str = a;file >> a;
            while( a >= '0'  && a <= '9' || a == '.')
        {               
            str.append(1,a);        
            file >> a;
        }
        cout << str << " number" <<endl ;
            str = "";
        }   
    }

    file.close();
    ReadBefore=true;
}

MainProgram:

// MainProgram.cpp
#include "TheHeader.h"
int main ()
{
    FileReader ReaderObject;
    ReaderObject.ReadFile();
         return 0;
}

I added a new concept, called a 'constructor' FileReader().
all you need to know right now is a construtor is run when you make a new object. So FileReader ReaderObject; will call FileReader().

I used a bool called ReadBefore for this example, Basically the ReadFile function will set ReadBefore to true after it has been run, after the first run it returns because ReadBefore ==true.
In short ReadFile will work only once per each class.

I hope this helps ^_^

Thank you very much for explaining, i understand more how to make header file.
I have question about the end of header file, sometimes we need to put #endif, so is it necessary to put endif in header file or not?

Thank you very much for explaining, i understand more how to make header file.
I have question about the end of header file, sometimes we need to put #endif, so is it necessary to put endif in header file or not?

#endif, #if, and other such commands do have uses, but are not needed.
If you are going to include your header more than once at the top of the .h use #pragma once, that is all you should need.

Thank you very much for explaining, i understand more how to make header file.
I have question about the end of header file, sometimes we need to put #endif, so is it necessary to put endif in header file or not?

http://en.wikipedia.org/wiki/Include_guard

If you are going to include your header more than once at the top of the .h use #pragma once, that is all you should need.

...If it's available and implemented properly.
http://en.wikipedia.org/wiki/Pragma_once

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.