Need Header Help

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Need Header Help

 
0
  #1
Sep 29th, 2008
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
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include "Pen.h"
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[]) {
  8. Pen FavoritePen;
  9. FavoritePen.InkColor = blue;
  10. FavoritePen.ShellColor = clear;
  11. FavoritePen.CapColor = black;
  12. FavoritePen.Style = ballpoint;
  13. FavoritePen.Length = 6.0;
  14. FavoritePen.Brand = "Pilot";
  15. FavoritePen.InkLevelPercent = 90;
  16. Pen WorstPen;
  17. WorstPen.InkColor = blue;
  18. WorstPen.shellColor = red;
  19. WorstPen.CapColor = black;
  20. WOrstPen.Style = felt_tip;
  21. WorstPen.Length = 3.5
  22. WorstPen.Brand = "Acme Special";
  23. WorstPen.InkLevelPercent = 100;
  24. cout << "This is my favorite pen" <<endl;
  25. cout << "Color: " << FavoritePen.InkColor << endl;
  26. cout << "Brand: " << FavoritePen.Brand << endl;
  27. cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
  28. FavoritePen.write_on_paper("Hello, I am a pen");
  29. cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
  30. system("PAUSE");
  31. return 0;
  32. }

Pen.h
  1. #include <string>
  2. enum Color {blue, red, black, clear};
  3. enum Penstyle {ballpoint, felt_tip, fountain_pen};
  4. class Pen {
  5. public:
  6. Color InkColor;
  7. Color ShellColor;
  8. Color CapColor;
  9. PenStyle Style;
  10. float Length;
  11. string Brand;
  12. int InkLevelPercent;
  13. void write_on_paper(string words) {
  14. if (InkLevelPercent <=0) {
  15. cout << "Oops! Out of ink!" << endl;
  16. }
  17. else {
  18. cout << words << endl;
  19. InkLevelPercent = InkLevelPercent - words.length();
  20. }
  21. }
  22. void break_in_half() {
  23. InkLevelPercent = InkLvelPercent / 2;
  24. Length / 2.0;
  25. }
  26. void run_out_of_ink() {
  27. InkLevelPercent = 0;
  28. }
  29. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,660
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Need Header Help

 
0
  #2
Sep 29th, 2008
>>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.
Last edited by Ancient Dragon; Sep 29th, 2008 at 11:34 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Need Header Help

 
0
  #3
Sep 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Re: Need Header Help

 
0
  #4
Sep 30th, 2008
I included it:

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4.  
  5. using namespace std;

And it still doesn't work correctly. Is there anything else that I might have done wrong?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Need Header Help

 
0
  #5
Sep 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Re: Need Header Help

 
0
  #6
Sep 30th, 2008
Originally Posted by stilllearning View Post
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 =)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 477 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC