Thread: quick question

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    quick question

    Hi

    i am very new to c++

    which is the best way to write code?



    Code:
    std::cout << "asdf\n";

    or to just save code and use:

    Code:
    using namespace std;
    I have been warned against using the latter, but it wasnt explained to me. Why would the first be better

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Can't advise without some context. You generally want to limit the scope into which you dump the entire std (or any other) namespace.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I write "using namespace std;" coz I don't want to type

    std::cout<<

    std::cin>>

    std::cout<<

    again and again....

    They have done a thread on this if you're interested. I'll go and read that too.
    http://cboard.cprogramming.com/showthread.php?t=61210

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    i am very new to c++
    Then these issues don't matter to you. First learn about functions, classes and objects, then go into these minor details which only matter when creating and including your own header files, i.e. when you want to create a library.
    First create a program...

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    std::string name("Manav");
    std::cout << name << std::endl;
    :: (four dots) look very cute to me!
    Last edited by manav; 05-14-2008 at 07:30 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also do...
    using std::cout;
    using std::cin;

    And you won't have to worry about that either. It will make it possible to just type "cin" or "cout".
    Many would argue against importing namespaces, but that's an entirely different issue. As long as you're starting out, it will probably not be of an issue to you.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    what's the need for std and using etc?

    just do #include<iostream.h> <-- it already has cin and cout functions
    and you can simple do
    Code:
    cout<<"what ever";
    cin>>a;
    just an example ;\

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    salmansalman, <iostream.h> is a pre-standard header.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by salmansalman View Post
    what's the need for std and using etc?

    just do #include<iostream.h> <-- it already has cin and cout functions
    and you can simple do
    Code:
    cout<<"what ever";
    cin>>a;
    just an example ;\
    Because what you suggest os not conforming to the C++ standard? It won't compile unless you are using a really old compiler [or a compiler that still supports old include headers].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    you can include iostream.h and it pretty much works with all old/new compiler .. and laserlight when did I said it's not? i just said the use of "using" and "std" can be avoided easily by including iostream.h :\

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by salmansalman View Post
    you can include iostream.h and it pretty much works with all old/new compiler .. and laserlight when did I said it's not? i just said the use of "using" and "std" can be avoided easily by including iostream.h :\
    No, that's not true. Some compilers (such as Visual Studio) does NOT have iostream.h any longer. It's deprecated, and nowmore unstandard. You should not use it under any circumstances.
    If you just want to use cin/cout without std:: first, then just do using namespace std;
    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.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    I have tc4.5 tc3.0 Visual studio and it all has iostream.h, i do asigments every now and then and I use vc++ sometimes but mostly tc4.5 .. but whenever I use vc++ it has no problems with iostream.h ;\

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by salmansalman View Post
    you can include iostream.h and it pretty much works with all old/new compiler .. and laserlight when did I said it's not? i just said the use of "using" and "std" can be avoided easily by including iostream.h :\
    This thread should be pinned as an example of why a good, highly recommended C++ book should always be preferred to an internet forum.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Error 1 fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory g:\w00t\visual studio 2008\projects\help\help2.cpp 2
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    you can include iostream.h and it pretty much works with all old/new compiler
    Kindly test the program below with the online Comeau compiler:
    Code:
    #include <iostream.h>
    
    int main()
    {
        cout<<"what ever";
        return 0;
    }
    and laserlight when did I said it's not? i just said the use of "using" and "std" can be avoided easily by including iostream.h :\
    True, you omitted to warn that what you suggested is wrong.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM