quick question

This is a discussion on quick question within the C++ Programming forums, part of the General Programming Boards category; Hi i am very new to c++ which is the best way to write code? Code: std::cout << "asdf\n"; or ...

  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.
    std::cout or using namespace std or using std::cout

  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 08:30 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    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
    22,166
    salmansalman, <iostream.h> is a pre-standard header.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    22,918
    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
    Posts
    22,918
    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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21