View Poll Results: Which code is better written?

Voters
1. You may not vote on this poll
  • Example 1

    0 0%
  • Example 2

    1 100.00%
  • It's the same

    0 0%

Two line do the same but are different (cout). Why?

This is a discussion on Two line do the same but are different (cout). Why? within the C++ Programming forums, part of the General Programming Boards category; Could you help me out understand the correct way to write c++? I have 2 hello world examples: Example 1: ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Two line do the same but are different (cout). Why?

    Could you help me out understand the correct way to write c++?

    I have 2 hello world examples:

    Example 1:
    Code:
    #include <iostream>
    
    int main (int argc, char * const argv[]) {
        // insert code here...
        cout << "Hello, World!\n";
        return 0;
    }
    Example 2:
    Code:
    #include <iostream> 
    using namespace std; 
     
    int main () 
    { 
      cout << "Hello World!"; 
      return 0; 
    }
    They both do the same obviously but should it be done one way or the other or it doesn't matter (please explain way).

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Quote Originally Posted by kotoko
    They both do the same obviously but should it be done one way or the other or it doesn't matter (please explain way).
    It is not obvious that they do the same thing. The first program should not compile since the name cout is used, but has not been declared. The second program should compile, as cout refers to std::cout by virtue of the using directive using namespace std;.
    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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Hmm, I said they do the same thing because I was able to run them both and got the same output :S

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Considering that you should not even be able to compile the first program, I find that hard to believe.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New, making a survey program
    By shaffer in forum C++ Programming
    Replies: 18
    Last Post: 12-01-2006, 11:36 AM
  2. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM

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