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%

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

  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
    28,413
    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;.
    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

  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
    28,413
    Considering that you should not even be able to compile the first program, I find that hard to believe.
    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. 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, 01: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