Thread: New to C++

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    New to C++

    Hi, I am new to C++ programming. I bought C++ How to program Pearson International Edition by Deitel & Deitel. All the codes that were provided in the book doesn't seems to work in my compiler. I don't know much about compilers in C++. I have downloaded DEVC++ 5.0 but the code I used from the book doesn't work. I've downloaded Turbo C++ 3.0 and 4.5 and used it but still won't accept the code that is from the book.

    I typed in this lines of codes from the book.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <string>
    using std::string;
    using std::getlin;
    
    class GradeBook
    {
       public: 
                  void setCourseName( string name )
                  {
                         courseName = name;
                  }
                  string getCourseName()
                  {
                         return courseName;
                  }
                  void displayMessage()
                  {
                         cout << "Welcome to the gradebook for \n" << getCourseName() << "!" 
    };
    
    int main()
    {
         string nameOfCourse;
       
         GradeBook myGradeBook; 
       
         cout << "Initial course name is: " << myGradeBook.getCourseName() << endl;
    
    cout << "Please enter the course name" << endl;
    getline( cin, nameOfCourse );
    myGradeBook.setCourseName( nameOfCourse );
    
    cout << endl;
    myGradeBook.displayMessage();
    }
    I have checked the codes many times and compared it from the book, I just retyped it right here to show it to all of you.. When I compiled it using Turbo C++ 3.0, 4.5 it displayed lots of error messages and as well as in DevC++.

    Like:
    Unable to open include file 'IOSTREAM'
    Declaration syntax error
    Variable using is initialized more than once
    Declaratino syntax error
    Variable using is initialized more than once
    Declaratino syntax error
    Unable to open include file 'STRING'
    Variable using is initialized more than once
    Declaratino syntax error
    'string' cannot start a parameter declaration
    type name expected
    Declaration missing ;
    Undefined symbol 'courseName'
    and lots more

    I typed in a small code..

    Code:
    #include <iostream>
    #include <string>
    
    int main( void )
    {
          string name;
          cout << "Enter name: "; getline( cin, name );
          return 0;
    }
    It produced an error message still:

    Unable to open include file 'IOSTREAM'
    Unable to open include file 'STRING'
    Undefined symbol 'string'
    Statement missing;
    Undefined symbol 'cin'
    Undefined symbol 'name'
    Function 'getline' should have a prototype
    Undefined symbol 'name'

    I cannot really understand what's happening. I've checked the include and library directories.. They are all correct because I was able to input some C codes and compiled it successfully but when I input C++ codes using the book I have it's not working, it's showing hundreds of error messages.

    I hope that someone could help me with this.. Hope to hear from all of the masters in C++...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't surprise me since some of your code is wrong and Turbo C++ is outdated, non-conforming.
    OK, first step: get a real IDE & compiler. My recommendation is Visual Studio. If you want other suggestions, have a look here: http://cpwiki.sf.net/IDE

    And secondly, fix the code.
    Code:
    class GradeBook
    {
       public: 
                  void setCourseName( string name )
                  {
                         courseName = name;
                  }
                  string getCourseName()
                  {
                         return courseName;
                  }
                  void displayMessage()
                  {
                         cout << "Welcome to the gradebook for \n" << getCourseName() << "!";
                  }
    };
    That should do it. Good luck with your studies.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    DevC++ 5.0 im guessing is wingetx version. If so you need to create a C++ project not just a standard source file.

    But like Elysia said go for VC++ 2008 its much more up to date and standard compliant. Try to avoid DevC++ its really old now and is very buggy.
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Thanks for the reply guys.. I really appreciate it a lot. I downloaded Netbeans IDE for C++.. I hope this is not too much, do you think that's recommended?

    Elysia: I've corrected the code but still the same error. What I'm thinking is, why is it always saying unable to open the include file 'IOSTREAM' and some header that I use, I'm just referring to the book.

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Could anyone tell me why he has all those 'using' there?
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <string>
    using std::string;
    using std::getlin;
    I usually just use 'using namespace std;' is that outdated or something?
    Currently research OpenGL

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Using namespace std; brings in the entire std namespace into your namespace, while using std::cout only brings in the one name.

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ohh... Well, I guess my laziness takes over in deciding which to use :P
    Currently research OpenGL

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, and please place your using declarations and directives (if any) after all your includes. $l4xklynx's sample code has three using declarations before #include <string>. Although it is unlikely to matter in this case, the problem is that the scope of the using declarations and using directives would then affect what was included, which can be a Bad Thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by $l4xklynx View Post
    Elysia: I've corrected the code but still the same error. What I'm thinking is, why is it always saying unable to open the include file 'IOSTREAM' and some header that I use, I'm just referring to the book.
    If you get an error saying can't include iostream, it simply means that you have a bad compiler, unconfigured compiler, using the compiler incorrectly or a messed up installation.
    I'm not familiar with your IDE / compiler (Turbo C++, however, should be avoided at all costs).
    With Visual Studio, it's all set up and ready to go when you install it.
    The code should compile directly, without the need for any modifications.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    He needs to declare "coursename" as a private string in his class!

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    As Elysia said you have a problem with the configuration of the compiler. Might is suggest you look into using newer compiler/IDE's. There are some really good programming-environments out there that also are free such as
    Microsoft Visual Studio 2008 Express
    Code::Blocks

    These are 2 very good programming-tools that wont give you the trouble you will get from TurboC++

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I recommend a high speed internet connection during your download of VC++ 2008 Express. There is an ISO version one can download. Just get that, the online installer is worthless since you will ultimately still probably want to have the ability to reinstall at a later time.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The online installer isn't that useless. Granted, I always avoid online installers if I can since typically downloading stuff is something a download manager does better than an installer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by $l4xklynx View Post
    Thanks for the reply guys.. I really appreciate it a lot. I downloaded Netbeans IDE for C++.. I hope this is not too much, do you think that's recommended?

    Elysia: I've corrected the code but still the same error. What I'm thinking is, why is it always saying unable to open the include file 'IOSTREAM' and some header that I use, I'm just referring to the book.
    Try #include <iostream.h>. Some compilers want .h behind it, some dont. (at least thats my experience)

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Quote Originally Posted by Zonakusu View Post
    Try #include <iostream.h>. Some compilers want .h behind it, some dont. (at least thats my experience)
    No. That is just a bad way at attempting to force a ........ty product to work. iostream.h is not a standard header and anything that uses that (along with alot of other headers that ends with .h) is prone to not work if anybody but you try to compile it.

    Save yourself ALOT of hassle later down the road and do it right from the start.

Popular pages Recent additions subscribe to a feed