Thread: Experienced C programmer lost in C++

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Experienced C programmer lost in C++

    I am using the book Teach Yourself C++ in 21 days by SAMS and I am writing my code in the Microsoft Visual C++ 6.0 Professional Edition compiler. I came across this simple example of cout which has made me go crazy

    Code:
    #include <iostream.h>
    
    int main()
    {
    	std::cout<<"Hello there.\n";
    	std::cout<<"Here is 5: "<<5<<"\n";
    	std::cout<<"The manipulator std::endl ";
    	std::cout<<"writes a new line to the screen.";
    	std::cout<<std::endl;
                    return 0;
    }
    When I compiler this it gives me tons of erros because of the std:: codes. I have never seen this before in C programming at all, and I've programmed in C for about a year now. When I take away all the std:: in front of the cout tags everything compiles except the "std::endl" part. It is supposed to be the ANSI way of making a \n for new line. The error I get is this:
    error C2653: 'std' : is not a class or namespace name

    Can someone first please explain why std:: is everywhere in this book. I've searched for hours in old textbooks and the infamous google, but I cannot find good explanations. All my friends say they never use std::...I can accept this, but I can't accept that it won't compiler since it is supposedly ANSI compliant. Can experienced C++ programmers please help me.

    Thanks

    Hern

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i believe you're supposed to use:

    Code:
    #include <iostream>

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Yes, but that is irrelavent and causes more errors when I do that...You disregarded the question intact.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    for now you could use
    Code:
    #include <iostream>
    using namespace std;
    with this, you don't have to use std:: at all, as all of the std:: are already included. so your code would look like:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout<<"Hello there.\n";
    	cout<<"Here is 5: "<<5<<"\n";
    	cout<<"The manipulator std::endl ";
    	cout<<"writes a new line to the screen.";
    	cout<<endl;
                    return 0;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    meh- http://www-h.eng.cam.ac.uk/help/tpl/...amespaces.html

    also, when you include iostream, you must have:

    Code:
    using namespace std;
    that link should help you a bit, it's pretty indepth

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can someone first please explain why std:: is everywhere in this book.
    Because everything in the standard library is contained in the std namespace. Your book uses one of three ways to get access to the names in the namespace.

    >I have never seen this before in C programming at all
    Get used to it. Modern C++ is wildly different than C.

    >Can experienced C++ programmers please help me.
    If your compiler supports it (which it should), use namespaces. They are an excellent way to avoid naming conflicts, but for smaller programs can be annoying. There are three ways to gain access to a namespace:
    Code:
    #include <iostream>
    
    using namespace std; // "Gimme everything"
    
    int main()
    {
      cout<<"Hello, world!"<<endl;
    }
    Code:
    #include <iostream>
    
    // "Only give me what I need, but still let me use simple syntax"
    using std::cout;
    using std::endl;
    
    int main()
    {
      cout<<"Hello, world!"<<endl;
    }
    Code:
    #include <iostream>
    
    // "I'll explicitly qualify names in the namespace, don't do anything special"
    
    int main()
    {
      std::cout<<"Hello, world!"<<std::endl;
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Thank you!!! My book made no mentioning of
    Code:
    using namespace std;
    Thanks to all who replied it works now
    I'm sure I'll be back to this board for many more questions.

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>My book made no mentioning of

    I suggest you place that book directly in the garbage can, hern. I personally hate the SAMS series....don't ask which book to use, just do a board search - that will give you many suggestions

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Axon,

    haha, I will once I finish reading it..Books are expensive...I just got a job making $6 / hr so until I save up this will have to do.

    Thanks for your help,

    An old tutor of mine who has programmed for the past 10 years recommended one by Herbert Shildt that I'm goign to buy in a few weeks...This will just give me enough of a base..

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Books are expensive
    Yes they are. Shop very carefully because a great many of them suck.

    >who has programmed for the past 10 years recommended one by Herbert Shildt
    *sigh* Don't read Schildt, ever. He earned his entry in the hacker jargon file. Might I suggest a thin book called Accelerated C++ by Koenig and Moo?
    My best code is written with the delete key.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Microsoft Visual C++ 6.0
    Does this old thing even know about namespaces?

    > An old tutor of mine who has programmed for the past 10 years recommended one by Herbert Shildt
    Those that can, do
    Those that can't, teach
    Those that can't teach, ghost-write for HS

    Are there actually any competent teachers in the world? Not only do a huge portion of them get page 1 stuff wrong (like saying main returns void), they also recommend crap books.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does this old thing even know about namespaces?
    Surprisingly enough, it does.

    >Those that can't teach, ghost-write for HS
    I thought every Schildt book was just a copy/paste of his first with some key words changed.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    27
    Weird, I use BCB5.5 and only have to put in #include <iostream.h>.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Many compilers in use today allow you to use either the iostream.h version or the iostream version. Some require you to use one version or the other. The current standard, however, is to use iostream without the .h if your compiler is compliant with that syntax. Trying to stay standard wherever you can is to be encouraged, even if it isn't necessary for a given compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are You An Experienced Programmer? Do You Like Football/soccer? (uk)
    By Mark Smulders in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 06-28-2007, 01:53 AM
  2. Looking for experienced programmer
    By Sam Granger in forum Projects and Job Recruitment
    Replies: 6
    Last Post: 02-04-2006, 08:05 AM
  3. Looking for experienced C++ Programmer
    By PsychoMan in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-08-2005, 04:32 PM
  4. Experienced C++ programmer look here please
    By kurdt in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 08-23-2004, 06:01 AM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM