| | |
Need Header Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
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
Pen.h
main.cpp
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
You are probably getting compile error messages on pen.h because you forgot to add
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.
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
I included it:
And it still doesn't work correctly. Is there anything else that I might have done wrong?
C++ Syntax (Toggle Plain Text)
#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?
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
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 =)
![]() |
Similar Threads
- PHP-Nuke Tutorial #1: Altering the Header Tags (PHP)
- Html header of a page changed programmatically? (ASP.NET)
- New header (DaniWeb Community Feedback)
- How to write a header file (C++)
- c vs h header files... (C)
- missing function header (C++)
Other Threads in the C++ Forum
- Previous Thread: I can call the function.. But not accurate anwer.
- Next Thread: errors in fifo module
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






