All right so I'm learning C++ via library books and the information isn't too helpful, but it's the best I can find. Most of the books at my library are older than I am - copyright 1993 - some even earlier. So it's kind of ironic how the [seemingly] simplest thing in C++ (including a header file) can possibly be so hard. I'm working on one of the examples from the book - C++ For Dummies. I copied down an example about classes and objects, meanwhile the main problem is finding out how to include the header file. Here's the main and the header file. It would greatly appreciate me if someone were to please help me. Thanks in advance! =)

main.cpp

#include <iostream>
#include <cstdlib>
#include "Pen.h"

using namespace std;

int main(int argc, char *argv[]) {
    Pen FavoritePen;
    FavoritePen.InkColor = blue;
    FavoritePen.ShellColor = clear;
    FavoritePen.CapColor = black;
    FavoritePen.Style = ballpoint;
    FavoritePen.Length = 6.0;
    FavoritePen.Brand = "Pilot";
    FavoritePen.InkLevelPercent = 90;
    Pen WorstPen;
    WorstPen.InkColor = blue;
    WorstPen.shellColor = red;
    WorstPen.CapColor = black;
    WOrstPen.Style = felt_tip;
    WorstPen.Length = 3.5
    WorstPen.Brand = "Acme Special";
    WorstPen.InkLevelPercent = 100;
    cout << "This is my favorite pen" <<endl;
    cout << "Color: " << FavoritePen.InkColor << endl;
    cout << "Brand: " << FavoritePen.Brand << endl;
    cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
    FavoritePen.write_on_paper("Hello, I am a pen");
    cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;    
    system("PAUSE");
    return 0;
}

Pen.h

#include <string>
enum Color {blue, red, black, clear};
enum Penstyle {ballpoint, felt_tip, fountain_pen};
class Pen {
public:
             Color InkColor;
             Color ShellColor;
             Color CapColor;
             PenStyle Style;
             float Length;
             string Brand;
             int InkLevelPercent;
             void write_on_paper(string words) {
                  if (InkLevelPercent <=0) {
                        cout << "Oops! Out of ink!" << endl;
                        }
                     else {
                        cout << words << endl;
                        InkLevelPercent = InkLevelPercent - words.length();
                        }
                        }
             void break_in_half() {
                  InkLevelPercent = InkLvelPercent / 2;
                  Length / 2.0;
                  }
             void run_out_of_ink() {
                  InkLevelPercent = 0;
                  }
                  };

>>meanwhile the main problem is finding out how to include the header file

But the code you posted does it correctly #include <iostream> is all that is needed. The the compiler starts reading your program the #include statement causes it to open up the include file and add its contents to the *.cpp file that is being compiled. That is part of the preprocessor operations.

You are probably getting compile error messages on pen.h because you forgot to add using namespace std; after including <string> or you can use std::string Brand; to declare the namespace.

That is how you include the header file.

You will also need to include <iostream> in your Pen.h files since there are some print statements cout in there. So you may want to move that to the Pen.h header file.

I included it:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

And it still doesn't work correctly. Is there anything else that I might have done wrong?

Can you post the compile errors that you are getting ?

A couple of silly things like WOrstPen.Style = felt_tip; .. spelling mistake

also Length / 2.0; .. you need to assign this to some variable.

Can you post the compile errors that you are getting ?

A couple of silly things like WOrstPen.Style = felt_tip; .. spelling mistake

also Length / 2.0; .. you need to assign this to some variable.

Yeah, I know. I just caught that spelling mistake. By the way, there's no mistake in Length / 2.0;. It's fine. Anyway, all of the problems are typos. It turns out that when it highlights the line of code that includes the header, the compiler means that there is a problem inside the header, not with the including line itself. Sorry if you guys didn't catch the 7 typos, it took me like 30 times of reading it over to get it. Thanks for your time =)

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.