943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 586
  • C++ RSS
Sep 29th, 2008
0

Need Header Help

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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. };
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Sep 29th, 2008
0

Re: Need Header Help

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 29th, 2008
0

Re: Need Header Help

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 30th, 2008
0

Re: Need Header Help

I included it:

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Sep 30th, 2008
0

Re: Need Header Help

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 30th, 2008
0

Re: Need Header Help

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 =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I can call the function.. But not accurate anwer.
Next Thread in C++ Forum Timeline: errors in fifo module





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC